Skip to content

Input type=file dialogue triggers windows focus refetch #2960

Closed

Description

@Dynalon

Describe the bug
Using an <input type="file" onChange={onChangeHandler} /> component will bring up a file chooser to chose your files (i.e. for uploads). The problem is, that at least on macOS/Chrome, once the dialog box disappears after you chose the file, this triggers the refetchOnFocus event (if enabled). This bug cost me hours to debug, as the onChangeHandler was never called - the refetch triggered a re-render and thus the component was unmounted (and remounted) before the onChangeHandler actually could fire.

To Reproduce
Steps to reproduce the behavior:

  1. Use any <input type="file" onChange={onChange} />
  2. Click on "Choose file" and select a file
  3. Observe the refetchOnFocus (possibly triggering a re-render)
  4. onChange handler is not fired

Expected behavior
The <input type="file" /> dialog should be exempted from the refetchOnFocus event

Desktop (please complete the following information):

  • OS: macOS
  • Browser: latest Chrome

Activity

  1. TkDodo commented on Nov 17, 2021

    @TkDodo
    Collaborator

    This seems to be similar to

    to detect changes in focus, we use browser native events . If opening a file upload dialog triggers one of these events in certain browsers, I'm not sure what we should do here.

    My recommendations from this comment would likely also apply here: Tweak the FocusManager to your needs or turn off refetchOnWindowFocus for this query.

  2. Dynalon commented on Nov 17, 2021

    @Dynalon
    ContributorAuthor

    I could work around that issue by just setting { refetchOnFocus: false } for all the queries present on the site with the input dialogue, but I think this is bound to fail again in the future (when introducing a new query i.e. in something as omnipresent as the navbar).

    It might make sense to highlight this in the documentation about refetchOnFocus. It would also be nice if there was a quick/simple API to temporarily disabled the refetchOnFocus App-wide so we can use it in an effect for a component. I tried using queryClient.setDefaultOptions() but changing the values for refetchOnFocus seem to itself trigger a refetch.

    Proposal:

    const queryClient = useQueryClient()
    React.useEffect(() => {
        queryClient.disableRefetchOnFocus()
         // dispose function triggered on unmount
        return () => queryClient.enableRefetchOnFocus()
    })

    And of course, the above snippet could also be put into a custom hook and would boil down to:

    // put in any component that uses a <input type="file" /> or alert()
    useDisableRefetchOnFocusTemporarily()
  3. TkDodo commented on Nov 17, 2021

    @TkDodo
    Collaborator

    have a look at focusManager.setFocused. As far as I can see, if you set:

    focuseManager.setFoused(true)
    

    before you open the file dialog, followed by

    focuseManager.setFoused(undefined)
    

    it should revert back to the normal focusManager behaviour

  4. Dynalon commented on Nov 17, 2021

    @Dynalon
    ContributorAuthor

    @TkDodo thanks so much for your help! I could come up with this Hook now that I call in components that have a <input type="file" /> dialogue to disable the refetchOnFocus for as long as that component is mounted:

    import { useEffect } from "react"
    import { focusManager } from "react-query"
    
    export function useDisableRefetchOnFocus() {
      useEffect(() => {
        focusManager.setFocused(false)
        return () => focusManager.setFocused(undefined)
      }, [])
    }
  5. TkDodo commented on Nov 17, 2021

    @TkDodo
    Collaborator

    That looks good indeed. If you want to contribute this to the FocusManager docs as a known gotcha, please go ahead :)

  6. kyawthura-gg commented on Jan 12, 2023

    @kyawthura-gg

    Looks like only happens in dev. In production, there’s no issue. Just tested, it's same in production.

  7. TkDodo commented on Jan 12, 2023

    @TkDodo
    Collaborator
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