📄 tanstack/query/v5/docs/framework/react/examples/optimistic-updates-cache

File: optimistic-updates-cache.md | Updated: 11/15/2025

Source: https://tanstack.com/query/v5/docs/framework/react/examples/optimistic-updates-cache



TanStack

Query v5v5

Search...

+ K

Auto

Log In

TanStack StartRC

Docs Examples GitHub Contributors

TanStack Router

Docs Examples GitHub Contributors

TanStack Query

Docs Examples GitHub Contributors

TanStack Table

Docs Examples Github Contributors

TanStack Formnew

Docs Examples Github Contributors

TanStack DBbeta

Docs Github Contributors

TanStack Virtual

Docs Examples Github Contributors

TanStack Paceralpha

Docs Examples Github Contributors

TanStack Storealpha

Docs Examples Github Contributors

TanStack Devtoolsalpha

Docs Github Contributors

More Libraries

Maintainers Partners Support Learn StatsBETA Discord Merch Blog GitHub Ethos Brand Guide

Documentation

Framework

React logo

React

Version

v5

Search...

+ K

Menu

Getting Started

Guides & Concepts

API Reference

ESLint

Examples

Plugins

Framework

React logo

React

Version

v5

Menu

Getting Started

Guides & Concepts

API Reference

ESLint

Examples

Plugins

React Example: Optimistic Updates Cache

Github StackBlitz CodeSandbox

=========================================================================================================================================================================================================================================================================================================================================================================================================================================================================

Code ExplorerCode

Interactive SandboxSandbox

  • src

    • pages

      • api

      • index.tsx file iconindex.tsx

  • .gitignore file icon.gitignore

  • README.md file iconREADME.md

  • next-env.d.ts file iconnext-env.d.ts

  • next.config.js file iconnext.config.js

  • package.json file iconpackage.json

  • tsconfig.json file icontsconfig.json

tsx

import * as React from 'react'

import {
  QueryClient,
  QueryClientProvider,
  queryOptions,
  useMutation,
  useQuery,
  useQueryClient,
} from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'

const client = new QueryClient()

type Todos = {
  items: ReadonlyArray<{
    id: string
    text: string
  }>
  ts: number
}

async function fetchTodos({ signal }: { signal: AbortSignal }): Promise<Todos> {
  const response = await fetch('/api/data', { signal })
  return await response.json()
}

const todoListOptions = queryOptions({
  queryKey: ['todos'],
  queryFn: fetchTodos,
})

function Example() {
  const queryClient = useQueryClient()
  const [text, setText] = React.useState('')
  const { isFetching, ...queryInfo } = useQuery(todoListOptions)

  const addTodoMutation = useMutation({
    mutationFn: async (newTodo: string) => {
      const response = await fetch('/api/data', {
        method: 'POST',
        body: JSON.stringify({ text: newTodo }),
        headers: { 'Content-Type': 'application/json' },
      })
      return await response.json()
    },
    // When mutate is called:
    onMutate: async (newTodo, context) => {
      setText('')
      // Cancel any outgoing refetch
      // (so they don't overwrite our optimistic update)
      await context.client.cancelQueries(todoListOptions)

      // Snapshot the previous value
      const previousTodos = context.client.getQueryData(
        todoListOptions.queryKey,
      )

      // Optimistically update to the new value
      if (previousTodos) {
        context.client.setQueryData(todoListOptions.queryKey, {
          ...previousTodos,
          items: [\
            ...previousTodos.items,\
            { id: Math.random().toString(), text: newTodo },\
          ],
        })
      }

      return { previousTodos }
    },
    // If the mutation fails,
    // use the result returned from onMutate to roll back
    onError: (err, variables, onMutateResult, context) => {
      if (onMutateResult?.previousTodos) {
        context.client.setQueryData<Todos>(
          ['todos'],
          onMutateResult.previousTodos,
        )
      }
    },
    // Always refetch after error or success:
    onSettled: (data, error, variables, onMutateResult, context) =>
      context.client.invalidateQueries({ queryKey: ['todos'] }),
  })

  return (
    <div>
      <p>
        In this example, new items can be created using a mutation. The new item
        will be optimistically added to the list in hopes that the server
        accepts the item. If it does, the list is refetched with the true items
        from the list. Every now and then, the mutation may fail though. When
        that happens, the previous list of items is restored and the list is
        again refetched from the server.
      </p>
      <form
        onSubmit={(e) => {
          e.preventDefault()
          addTodoMutation.mutate(text)
        }}
      >
        <input
          type="text"
          onChange={(event) => setText(event.target.value)}
          value={text}
        />
        <button disabled={addTodoMutation.isPending}>Create</button>
      </form>
      <br />
      {queryInfo.isSuccess && (
        <>
          <div>
            {/* The type of queryInfo.data will be narrowed because we check for isSuccess first */}
            Updated At: {new Date(queryInfo.data.ts).toLocaleTimeString()}
          </div>
          <ul>
            {queryInfo.data.items.map((todo) => (
              <li key={todo.id}>{todo.text}</li>
            ))}
          </ul>
          {isFetching && <div>Updating in background...</div>}
        </>
      )}
      {queryInfo.isLoading && 'Loading'}
      {queryInfo.error instanceof Error && queryInfo.error.message}
    </div>
  )
}

export default function App() {
  return (
    <QueryClientProvider client={client}>
      <Example />
      <ReactQueryDevtools initialIsOpen />
    </QueryClientProvider>
  )
}


import * as React from 'react'

import {
  QueryClient,
  QueryClientProvider,
  queryOptions,
  useMutation,
  useQuery,
  useQueryClient,
} from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'

const client = new QueryClient()

type Todos = {
  items: ReadonlyArray<{
    id: string
    text: string
  }>
  ts: number
}

async function fetchTodos({ signal }: { signal: AbortSignal }): Promise<Todos> {
  const response = await fetch('/api/data', { signal })
  return await response.json()
}

const todoListOptions = queryOptions({
  queryKey: ['todos'],
  queryFn: fetchTodos,
})

function Example() {
  const queryClient = useQueryClient()
  const [text, setText] = React.useState('')
  const { isFetching, ...queryInfo } = useQuery(todoListOptions)

  const addTodoMutation = useMutation({
    mutationFn: async (newTodo: string) => {
      const response = await fetch('/api/data', {
        method: 'POST',
        body: JSON.stringify({ text: newTodo }),
        headers: { 'Content-Type': 'application/json' },
      })
      return await response.json()
    },
    // When mutate is called:
    onMutate: async (newTodo, context) => {
      setText('')
      // Cancel any outgoing refetch
      // (so they don't overwrite our optimistic update)
      await context.client.cancelQueries(todoListOptions)

      // Snapshot the previous value
      const previousTodos = context.client.getQueryData(
        todoListOptions.queryKey,
      )

      // Optimistically update to the new value
      if (previousTodos) {
        context.client.setQueryData(todoListOptions.queryKey, {
          ...previousTodos,
          items: [\
            ...previousTodos.items,\
            { id: Math.random().toString(), text: newTodo },\
          ],
        })
      }

      return { previousTodos }
    },
    // If the mutation fails,
    // use the result returned from onMutate to roll back
    onError: (err, variables, onMutateResult, context) => {
      if (onMutateResult?.previousTodos) {
        context.client.setQueryData<Todos>(
          ['todos'],
          onMutateResult.previousTodos,
        )
      }
    },
    // Always refetch after error or success:
    onSettled: (data, error, variables, onMutateResult, context) =>
      context.client.invalidateQueries({ queryKey: ['todos'] }),
  })

  return (
    <div>
      <p>
        In this example, new items can be created using a mutation. The new item
        will be optimistically added to the list in hopes that the server
        accepts the item. If it does, the list is refetched with the true items
        from the list. Every now and then, the mutation may fail though. When
        that happens, the previous list of items is restored and the list is
        again refetched from the server.
      </p>
      <form
        onSubmit={(e) => {
          e.preventDefault()
          addTodoMutation.mutate(text)
        }}
      >
        <input
          type="text"
          onChange={(event) => setText(event.target.value)}
          value={text}
        />
        <button disabled={addTodoMutation.isPending}>Create</button>
      </form>
      <br />
      {queryInfo.isSuccess && (
        <>
          <div>
            {/* The type of queryInfo.data will be narrowed because we check for isSuccess first */}
            Updated At: {new Date(queryInfo.data.ts).toLocaleTimeString()}
          </div>
          <ul>
            {queryInfo.data.items.map((todo) => (
              <li key={todo.id}>{todo.text}</li>
            ))}
          </ul>
          {isFetching && <div>Updating in background...</div>}
        </>
      )}
      {queryInfo.isLoading && 'Loading'}
      {queryInfo.error instanceof Error && queryInfo.error.message}
    </div>
  )
}

export default function App() {
  return (
    <QueryClientProvider client={client}>
      <Example />
      <ReactQueryDevtools initialIsOpen />
    </QueryClientProvider>
  )
}

Optimistic Updates (UI)

Pagination

Partners Become a Partner

Code RabbitCode Rabbit CloudflareCloudflare AG GridAG Grid NetlifyNetlify NeonNeon WorkOSWorkOS ClerkClerk ConvexConvex ElectricElectric SentrySentry PrismaPrisma StrapiStrapi UnkeyUnkey

[###### Want to Skip the Docs?

Query.gg - The Official React Query Course
\

“If you’re serious about *really* understanding React Query, there’s no better way than with query.gg”—Tanner Linsley

Learn More](https://query.gg/?s=tanstack)

scarf analytics