Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Invalidating and Refetching Queries | Updating Data and Advanced Query Features
React Query Essentials

bookInvalidating and Refetching Queries

When your application updates data on the server using a mutation, the local cached data in React Query may become outdated. This can cause your UI to display stale information until the next time the query runs. To ensure users always see the freshest data, you need a way to tell React Query that certain cached queries are now invalid and should be refetched from the server. This process is called query invalidation. Invalidating queries is essential after mutations because it keeps your UI in sync with the server, avoids displaying incorrect data, and provides a better user experience.

To trigger a refetch of specific queries after a mutation, you use the queryClient.invalidateQueries method. This method marks the specified queries as invalid, prompting React Query to refetch them the next time they are used or when the component remounts. Here is how you might use it in a mutation's onSuccess callback:

import { useMutation, useQueryClient } from "@tanstack/react-query";

function MyComponent() {
  const queryClient = useQueryClient();

  const mutation = useMutation({
    mutationFn: (newData) =>
      fetch("/api/data", {
        method: "POST",
        body: JSON.stringify(newData),
      }),
    onSuccess: () => {
      // Invalidate and refetch the "data" query after mutation succeeds
      queryClient.invalidateQueries({ queryKey: ["data"] });
    },
  });

  // ...UI code
}

In this example, whenever the mutation succeeds, the "data" query is invalidated and will be refetched, ensuring the UI displays the latest information from the server.

question mark

When should you use queryClient.invalidateQueries in a React Query workflow?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookInvalidating and Refetching Queries

Swipe to show menu

When your application updates data on the server using a mutation, the local cached data in React Query may become outdated. This can cause your UI to display stale information until the next time the query runs. To ensure users always see the freshest data, you need a way to tell React Query that certain cached queries are now invalid and should be refetched from the server. This process is called query invalidation. Invalidating queries is essential after mutations because it keeps your UI in sync with the server, avoids displaying incorrect data, and provides a better user experience.

To trigger a refetch of specific queries after a mutation, you use the queryClient.invalidateQueries method. This method marks the specified queries as invalid, prompting React Query to refetch them the next time they are used or when the component remounts. Here is how you might use it in a mutation's onSuccess callback:

import { useMutation, useQueryClient } from "@tanstack/react-query";

function MyComponent() {
  const queryClient = useQueryClient();

  const mutation = useMutation({
    mutationFn: (newData) =>
      fetch("/api/data", {
        method: "POST",
        body: JSON.stringify(newData),
      }),
    onSuccess: () => {
      // Invalidate and refetch the "data" query after mutation succeeds
      queryClient.invalidateQueries({ queryKey: ["data"] });
    },
  });

  // ...UI code
}

In this example, whenever the mutation succeeds, the "data" query is invalidated and will be refetched, ensuring the UI displays the latest information from the server.

question mark

When should you use queryClient.invalidateQueries in a React Query workflow?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
some-alt