0
Fork 0
mirror of https://github.com/TanStack/query.git synced 2026-11-16 22:50:20 +00:00
query/examples/react/default-query-function
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 default query function example to vite (#3954) 2022-08-11 17:54:51 +02:00
src fix(examples/react): throw on non-ok 'fetch' responses in the query and mutation functions (#12058) 2026-10-17 18:06:09 +02:00
.gitignore chore(ci): filter out examples (#4239) 2022-09-28 20:53:23 +02:00
eslint.config.js chore(examples): use 'defineConfig' from 'eslint/config' and enable '@ts-check' in 'eslint.config.js' (#12008) 2026-10-14 10:01:19 +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 Default Query Function Example

Sets a default queryFn on the QueryClient that builds the request URL from the query key, so each query only passes a queryKey.

How it works

  • src/index.tsx defines defaultQueryFn, which fetches https://jsonplaceholder.typicode.com${queryKey[0]}, and registers it as defaultOptions.queries.queryFn on the QueryClient.
  • In src/index.tsx, Posts calls useQuery<Array<Post>>({ queryKey: ['/posts'] }) and Post calls useQuery<Post>({ queryKey: [`/posts/${postId}`], enabled: !!postId }), without a queryFn.
  • In src/index.tsx, the list marks a post as visited when queryClient.getQueryData([`/posts/${post.id}`]) returns cached data.

To run this example:

  • npm install
  • npm run dev