RTK Query refetch on a button click shows the same data
Clicking the button runs the handler but no request appears in the Network tabShort answer
RTK Query serves cached data when a request has the same arguments, and sends no new request. A button that only calls the query again, or changes nothing in the arguments, gets the cache. Use the hook's refetch function, force the refetch, or invalidate cache tags.
Fix it fast
1Find out which cause you have
Click the button with the Network tab open. No new request means the answer came from the cache. A new request with the same data means the server returned the same data.
// in the component const { data, refetch, isFetching } = useGetPostsQuery(); console.log({ isFetching, count: data?.length });2Match what you see, then jump to its fix
- 1
No network request when the button is clickedThe same arguments are served from the cacheFix - 2
The button handler calls dispatch(api.endpoints.getPosts.initiate(args))You dispatch the endpoint's initiate action without forcing itFix - 3
The list is stale after a POST or DELETE until you reload the pageA mutation changes data but the query cache is not invalidatedFix - 4
Opening the page a second time shows the earlier data firstThe cached data is old when the component mounts againFix
- 1
What does the error look like?
Clicking the button runs the handler but no request appears in the Network tabThe list on screen does not change after a refetch or after a mutation
Cause 1 of 4
The same arguments are served from the cache
1Check
A request that produces the same cache key is de-duplicated, and if the data already exists in the cache it is served and no request goes to the server.
grep -rn "useGet\|initiate(" src2Fix
Call the refetch function returned by the query hook. It forces a refetch of that query.
function Posts() {
const { data, refetch, isFetching } = useGetPostsQuery();
return (
<>
<button onClick={() => refetch()} disabled={isFetching}>Reload</button>
<ul>{data?.map((p) => <li key={p.id}>{p.title}</li>)}</ul>
</>
);
}3Verify
Each click now shows a request in the Network tab and isFetching turns true.
console.log(isFetching);Confirmed by redux.js.org
Cause 2 of 4
You dispatch the endpoint's initiate action without forcing it
1Check
initiate returns cached data for the same arguments unless forceRefetch is passed.
grep -rn "initiate(" src2Fix
Pass forceRefetch: true in the options.
dispatch(
api.endpoints.getPosts.initiate(undefined, { subscribe: false, forceRefetch: true })
);3Verify
A request is sent on every click.
console.log('clicked');Confirmed by redux.js.org
Cause 3 of 4
A mutation changes data but the query cache is not invalidated
1Check
RTK Query refetches queries automatically when a mutation invalidates a tag that the query provides. Without tags, nothing links the two.
grep -rn "providesTags\|invalidatesTags" src2Fix
Add a tag type, have the query provide it, and have the mutation invalidate it.
export const api = createApi({
baseQuery: fetchBaseQuery({ baseUrl: "/api" }),
tagTypes: ["Post"],
endpoints: (build) => ({
getPosts: build.query({ query: () => "posts", providesTags: ["Post"] }),
addPost: build.mutation({
query: (body) => ({ url: "posts", method: "POST", body }),
invalidatesTags: ["Post"],
}),
}),
});3Verify
After adding a post, the list refetches without a manual click.
console.log(isFetching);Confirmed by redux.js.org
Cause 4 of 4
The cached data is old when the component mounts again
1Check
By default, data stays in the cache for 60 seconds after the last subscriber goes away, and is shown again on the next mount.
grep -rn "keepUnusedDataFor\|refetchOnMountOrArgChange" src2Fix
Ask for a refetch on mount, or shorten how long unused data stays.
const { data } = useGetPostsQuery(undefined, { refetchOnMountOrArgChange: true });3Verify
Each mount sends a request.
console.log('mounted');Confirmed by redux.js.org
How do I stop it coming back?
- Use tags for every query that a mutation can change.
- Reach for refetch() only for user-triggered reloads, and tags for data changes.
- Set refetchOnMountOrArgChange only on queries that really need fresh data every time.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
invalidateQueries or refetch does nothing in React Query and React Native
data does not update after mutation
invalidateQueries only refetches queries whose key starts with the key you pass and that are in use.
4 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix