Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

RTK Query refetch on a button click shows the same data

ErrorClicking the button runs the handler but no request appears in the Network tab

Short 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.

  • Applies to Redux Toolkit 2, React Redux 9, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 });
  2. 2Match what you see, then jump to its fix

    1. 1No network request when the button is clickedThe same arguments are served from the cacheFix
    2. 2The button handler calls dispatch(api.endpoints.getPosts.initiate(args))You dispatch the endpoint's initiate action without forcing itFix
    3. 3The list is stale after a POST or DELETE until you reload the pageA mutation changes data but the query cache is not invalidatedFix
    4. 4Opening the page a second time shows the earlier data firstThe cached data is old when the component mounts againFix

What does the error look like?

  • Clicking the button runs the handler but no request appears in the Network tab
  • The 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(" src

2Fix

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

Did this fix it?

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(" src

2Fix

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

Did this fix it?

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" src

2Fix

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

Did this fix it?

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" src

2Fix

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.