0
Fork 0
mirror of https://github.com/TanStack/query.git synced 2026-11-16 22:50:20 +00:00
query/examples/react/basic
Wonsuk Choi 70441f460d
docs(examples): rewrite the READMEs, add the framework or router to example titles, and rename examples to '<feature>-<framework>' (#12583)
* docs(examples): describe each example in its README with a summary, the framework it is built with, how it works and the npm commands

* docs(examples): add the framework or router to the README heading and page title of examples that have variants

* docs(examples/react): rename the Next.js examples to '<feature>-nextjs-<pages|app>-router'

* docs(examples/react): update package names, docs links and labels, and the lockfile for the renamed Next.js examples

* docs(examples): rename 'svelte/sveltekit', 'solid/solid-start-streaming' and 'angular/router' to '<feature>-<framework>'

* docs(examples): correct README details on staleness, hydration, 'hasMore', streaming and dependent queries, and prefetch on 'focusin' in 'vue/prefetching-nuxt4'
History 2026-11-12 16:58:15 +09:00
..
public docs(examples): migrate examples/react/basic to vite (#3949) 2022-08-16 09:03:31 +02:00
src style(examples): write durations in 'count * unit * 1000' order (#12383) 2026-10-26 02:29:26 +09:00
.gitignore chore(ci): filter out examples (#4239) 2022-09-28 20:53:23 +02:00
eslint.config.js chore(examples/*/eslint.config): use 'flat/recommended-strict' from '@tanstack/eslint-plugin-query' (#12385) 2026-10-26 02:55:51 +09:00
index.html docs(examples): unify page titles to 'TanStack <Framework> Query <Name> Example' and remove scaffold leftovers (#12580) 2026-11-12 14:54:59 +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 chore(examples/*/tsconfig): include '*.config.ts' and '*.config.js' like the packages (#12408) 2026-10-27 09:11:08 +09:00
vite.config.ts docs(examples): Modernise React examples (#7704) 2024-07-10 11:52:01 +10:00

TanStack React Query Basic Example

A list of posts and a post detail view. Posts you have already opened are marked as visited from the query cache. The cache is persisted to localStorage.

How it works

  • src/services/posts/queries.ts defines postsQueries.list() and postsQueries.detail(id) with queryOptions, keyed under ['posts']. src/services/posts/api.ts fetches the posts from JSONPlaceholder.
  • src/index.tsx renders Posts with useQuery(postsQueries.list()) and Post with useQuery(postsQueries.detail(postId)). Both show "Background Updating..." while isFetching is true.
  • In src/index.tsx, the list marks a post as visited when queryClient.getQueryData(postsQueries.detail(post.id).queryKey) returns cached data.
  • src/index.tsx wraps the app in PersistQueryClientProvider with a createAsyncStoragePersister over window.localStorage, and its QueryClient sets gcTime to 24 hours so the persisted queries are kept.

To run this example:

  • npm install
  • npm run dev