0
Fork 0
mirror of https://github.com/TanStack/query.git synced 2026-11-16 22:50:20 +00:00
query/examples/react/offline-react-router
Wonsuk Choi cb62f4f2ed
chore(*): adopt '@tanstack/oxlint-config' and '@tanstack/oxfmt-config', and fix the newly reported lint errors (#12555)
* chore(*): adopt '@tanstack/oxlint-config' and '@tanstack/oxfmt-config', and fix the newly reported lint errors

* chore(pnpm-lock.yaml): run 'pnpm dedupe'

* chore(scripts): move 'oxlint/plugin.ts' to 'oxlint-plugin.ts' to match the flat layout of 'scripts'

* chore(pnpm-workspace.yaml): update '@typescript-eslint/parser' and '@typescript-eslint/rule-tester' to '^8.72.1' to match the deduped '@typescript-eslint/utils'
History 2026-11-16 22:25:25 +09:00
..
public docs(examples): use the TanStack Query emblem as the favicon in every example (#12578) 2026-11-12 13:33:29 +09:00
src chore(*): adopt '@tanstack/oxlint-config' and '@tanstack/oxfmt-config', and fix the newly reported lint errors (#12555) 2026-11-16 22:25:25 +09:00
.gitignore docs(examples): use each framework's template '.gitignore' in every example (#12592) 2026-11-13 10:57:29 +09:00
eslint.config.js chore(*): adopt '@tanstack/oxlint-config' and '@tanstack/oxfmt-config', and fix the newly reported lint errors (#12555) 2026-11-16 22:25:25 +09:00
index.html docs(examples): rewrite the READMEs, add the framework or router to example titles, and rename examples to '<feature>-<framework>' (#12583) 2026-11-12 16:58:15 +09:00
package.json ci: Version Packages (#12543) 2026-11-07 00:00:45 +01:00
README.md docs(examples): rewrite the READMEs, add the framework or router to example titles, and rename examples to '<feature>-<framework>' (#12583) 2026-11-12 16:58:15 +09:00
tsconfig.json docs(examples/{react,solid,vue}): add offline examples for TanStack Router, React Router and Solid Router, and replace the discontinued '@tanstack/react-location' (#12483) 2026-11-02 01:49:54 +09:00
vite.config.ts chore(*): adopt '@tanstack/oxlint-config' and '@tanstack/oxfmt-config', and fix the newly reported lint errors (#12555) 2026-11-16 22:25:25 +09:00

TanStack React Query Offline Example (React Router)

A movie list that keeps working offline. Queries and mutations are persisted, mutations made offline are paused, and they resume when the connection returns or after a reload.

Built with React Router.

How it works

  • src/services/movies/queries.ts defines moviesQueries.list() and moviesQueries.detail(id) with queryOptions, and moviesMutations.update() with mutationOptions, whose mutationFn cancels the detail query before posting the comment.
  • src/App.tsx wraps the router in PersistQueryClientProvider with a localStorage persister and registers moviesMutations.update() with queryClient.setMutationDefaults(moviesQueries.all(), ...), so mutations restored after a reload still have a mutationFn. Its onSuccess calls queryClient.resumePausedMutations() and then invalidateQueries().
  • The loader in src/routes/detail.tsx fetches the movie with queryClient.query only when it is not cached, onlineManager.isOnline() is true, and the cache is not being restored. Otherwise useQuery in the component handles it.
  • src/routes/detail.tsx updates the comment with useMutation and a mutationKey under ['movies'], writing it optimistically in onMutate with queryClient.setQueryData and rolling back in onError. While offline, the mutation isPaused and the page shows "mutation paused - offline". src/routes/list.tsx reads useQuery(moviesQueries.list()).
  • src/mocks.ts sets up an MSW worker that serves GET /movies, GET /movies/:id, and POST /movies/:id with a 500 ms delay.

To run this example:

  • npm install
  • npm run dev