Skip to content

infinite loop refetch when call window.alert inside request #2868

Closed

Description

@SvyatoslavPozhydaev

Describe the bug
Since refetchOnWindowFocus is set to true by default, then if you call window.alert inside a request or somewhere else, you get an infinite loop

To Reproduce
Steps to reproduce the behavior:

useQuery('key', () =>
  fetch('some url')
    .then(response => response.json())
    .catch(error => {
       alert(error);
    })
)

Activity

  1. McLovin77 commented on Nov 4, 2021

    @McLovin77

    It makes sense that it created an infinite loop in your case, because what is happening is that:

    1. Component mounts and useQuery called.
    2. useQuery fires the queryFn (with refetchOnWindowFocus true by default).
    3. queryFn calls window.alert, which causes blur on window (by blur I mean the opposite of focus 👀).
    4. That blur (from step 2) is then dismissed when you click on the button on the alert prompt, which then causes another window focus (as in you are now focusing back on the screen and not on the alert prompt), and we go back to step 2.

    What I suggest (And I'm sorry if it might sound too obvious to you) is not to use alerts in the queryFn, but in the onSuccess, onSettled or onMutate.

  2. TkDodo commented on Nov 6, 2021

    @TkDodo
    Collaborator

    I agree with @McLovin77 , but I don't think that the situation would be better with the callbacks. Yes, generally, it is better to put side-effects like showing alerts there, however, with the default staleTime of 0, you would still get another refetch because of the focus event.

    You can:

    1. turn off refetchOnWindowFocus to avoid the refetch altogether
    2. set a higher staleTime, because only stale queries will be refetched
    3. not use window.alert but a toast library like react-hot-toast
    4. tweak the FocusManager to your needs.

    I would go with number 3 :)

  3. s-r-x commented on Apr 13, 2022

    @s-r-x

    facing the same issue. i have a little abstraction over useMutation, that relies on window.confirm. it looks something like this

    onMutate() {                                                                                                                      
          if (args.confirm) {                                                                                         
            const confirmed = window.confirm("");
            if (!confirmed) throw new ConfirmError();
          }
        },

    was able to fix it with these lines

    onMutate() {                                                                                                                      
          if (args.confirm) {
            focusManager.setFocused(false);                                                        
            const confirmed = window.confirm("");
            setTimeout(() => focusManager.setFocused(undefined));
            if (!confirmed) throw new ConfirmError();
          }
        },

    hope it helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions