Performing optimistic updates
As we saw in Chapter 2, an optimistic update is a pattern used during an ongoing mutation where we update our UI to show how it will look after our mutation is finished, although our mutation is still not confirmed as complete.
Well, React Query allows you to perform optimistic updates, and it makes it extremely simple. All you need is to use the callback functions we saw in the previous sections.
Here is how to perform an optimist update using the useMutation
hook:
import axios from "axios"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { useState } from "react"; const fetchAllData = async () => { const { data } = await axios.get( `https://danieljcafonso.builtwithdark.com/name-api` ); return data; }; const createUser = async (user) => { return axios.post (`https...