How to invalidate query after mutations in React-Query

Introduction

Query invalidation is an essential part while working with React-Query. Usually, when there is a mutation in the app, there are queries related, and when a mutation succeeded, we need to invalidate the related queries, so we can fetch the last query including the last data from the mutation.


Example

Consider we have a simple todo app, as shown in the following picture:

How to invalidate query after mutations in React-Query
How to invalidate query after mutations in React-Query

This app consists of one query to fetch the todo’s, and one mutation to submit them.

// Query
const { status, data, error } = useQuery(['todos'], async () => {
  const res = await axios.get('/api/data');
  return res.data;
});

// Mutation
const addMutation = useMutation((value) => fetch(`/api/data?add=${value}`));

Now, to invalidate the query once the mutation submission succeeded, so we can fetch the last todo items, we need to use useMutation‘s onSuccess callback option, and call the queryClient.invalidateQueries(['todos']) inside of it, as the following:

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

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

  const { status, data, error } = useQuery(['todos'], async () => {
    const res = await axios.get('/api/data');
    return res.data;
  });

  const addMutation = useMutation((value) => fetch(`/api/data?add=${value}`), {
    onSuccess: () => queryClient.invalidateQueries(['todos']),
  });
.....

Opening the Network tab, it does showing the following requests:
How to invalidate query after mutations in React-Query
How to invalidate query after mutations in React-Query

A POST request to add the new todo item, followed by a GET request to refetch all the items and re-render them.


Full Source Code.

That’s it for How to invalidate query after mutations in React-Query

As always happy coding!

Photo from Unsplash.

Related Posts

How to Capture Screenshots with Puppeteer In NodeJS

How to Capture Screenshots with Puppeteer In NodeJS

To Capture Screenshots with Puppeteer: Launch a Browser Instance Navigate to the Web Page Capture the Screenshot Introduction: Puppeteer is a powerful Node.js library that allows developers…

How to Minimize Puppeteer Browser Window To Tray

How to Minimize Puppeteer Browser Window To Tray

Puppeteer is a powerful tool for automating tasks in headless or non-headless web browsers using JavaScript. While Puppeteer is often used to perform actions within a browser,…

Intercepting Responses in Node.js with Puppeteer

Intercepting Responses in Node.js with Puppeteer

Introduction: Puppeteer is a powerful Node.js library that provides a high-level API for controlling headless Chrome or Chromium browsers. It’s widely used for web scraping, automated testing,…

Mastering React Component Re-rendering in Jest

Mastering React Component Re-rendering in Jest

In this hands-on guide, we’ll explore the art of optimizing React component re-rendering within Jest tests. By combining theory with practical coding examples, you’ll gain a deep…

Eliminating Nesting Loops in React Rendering

Eliminating Nesting Loops in React Rendering

React has ushered in a new era of web application development with its component-based structure, promoting code reusability and maintainability. But as projects evolve, achieving optimal performance…

Exploring Type and Interface Usage in TypeScript

Exploring Type and Interface Usage in TypeScript

TypeScript has gained immense popularity by bridging the gap between dynamic JavaScript and static typing. Two of its fundamental features, “Type” and “Interface,” play pivotal roles in…

Leave a Reply

%d bloggers like this: