You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Input type=file dialogue triggers windows focus refetch #2960
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:
Use any <input type="file" onChange={onChange} />
Click on "Choose file" and select a file
Observe the refetchOnFocus (possibly triggering a re-render)
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):
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.
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:
constqueryClient=useQueryClient()React.useEffect(()=>{queryClient.disableRefetchOnFocus()// dispose function triggered on unmountreturn()=>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()
@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:
Activity
TkDodo commentedon Nov 17, 2021
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
FocusManagerto your needs or turn offrefetchOnWindowFocusfor this query.Dynalon commentedon Nov 17, 2021
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:
And of course, the above snippet could also be put into a custom hook and would boil down to:
TkDodo commentedon Nov 17, 2021
have a look at focusManager.setFocused. As far as I can see, if you set:
before you open the file dialog, followed by
it should revert back to the normal
focusManagerbehaviourDynalon commentedon Nov 17, 2021
@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 therefetchOnFocusfor as long as that component is mounted:TkDodo commentedon Nov 17, 2021
That looks good indeed. If you want to contribute this to the FocusManager docs as a known gotcha, please go ahead :)
kyawthura-gg commentedon Jan 12, 2023
Looks like only happens in dev. In production, there’s no issue.Just tested, it's same in production.TkDodo commentedon Jan 12, 2023
I hope this will be fixed in v5: