RTK Query refetchOnFocus not triggering after setupListeners — what else controls it?
0 reputation · 10 Sept 2026, 12:33 UTC
0 reputation · 10 Sept 2026, 12:33 UTC
I'm wiring up RTK Query (via createApi in Redux Toolkit) and want queries to refetch when the browser window regains focus. The documentation describes refetchOnFocus as opt-in, and that setupListeners(store.dispatch) must be called for focus/reconnect events to reach the store.
My uncertainty is about how the pieces interact before I commit to a configuration: the flag can be set globally on the API, per endpoint, or per hook call, and I'm unclear which level wins when they conflict, and whether setupListeners alone is sufficient or if each query must also explicitly opt in. I also want to understand how this interacts with cached data that is still within its subscription lifetime — does focus always force a network request, or can a fresh cache entry suppress it?
Assuming a current @reduxjs/toolkit 2.x release:
refetchOnFocus is enabled globally but disabled on one endpoint, which setting applies?To enable refetchOnFocus, you must both call setupListeners(store.dispatch) and opt-in via the refetchOnFocus flag. If setupListeners is active but no network request occurs, the most common cause is that the query hook is not currently mounted in the component tree, or the specific configuration level has disabled the behavior.
RTK Query follows a "most specific wins" hierarchy for the refetchOnFocus setting. If you have conflicting settings, they resolve in this order:
useGetUsersQuery(undefined, { refetchOnFocus: false })) take the highest priority.endpoints object in createApi.refetchOnFocus setting in the createApi configuration object.If you enable it globally but disable it on a specific endpoint, that endpoint will not refetch on focus.
A window focus event does not automatically force a network request for every single cached item. Two conditions must be met for a refetch to trigger:
refetchOnFocus flag must be true for that specific query.Unlike refetchOnReconnect, which often focuses on restoring connectivity, refetchOnFocus is designed to refresh the UI. If the flag is true and the hook is mounted, RTK Query will trigger a refetch regardless of whether the cache entry is still within its keepUnusedDataFor lifetime.
You can verify if the listener is working independently of the network request by monitoring the Redux DevTools action log. When the window regains focus, look for the following action:
api/executeMutationorapi/executeQuery(triggered by the internalrefetchOnFocuslogic)
If you see the focus-related action dispatched but no network request follows, the issue is likely that the query is not currently subscribed (mounted) or the endpoint-level flag is false.
Are you running the application inside an iframe or a specialized webview? Window focus events may not bubble correctly in these environments, preventing setupListeners from detecting the event.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.