Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

README.md

14 lines781 Bytes

1# TanStack React Query Default Query Function Example
2​
3Sets a default `queryFn` on the `QueryClient` that builds the request URL from the query key, so each query only passes a `queryKey`.
4​
5## How it works
6​
7- `src/index.tsx` defines `defaultQueryFn`, which fetches `https://jsonplaceholder.typicode.com${queryKey[0]}`, and registers it as `defaultOptions.queries.queryFn` on the `QueryClient`.
8- In `src/index.tsx`, `Posts` calls `useQuery<Array<Post>>({ queryKey: ['/posts'] })` and `Post` calls ``useQuery<Post>({ queryKey: [`/posts/${postId}`], enabled: !!postId })``, without a `queryFn`.
9- In `src/index.tsx`, the list marks a post as visited when ``queryClient.getQueryData([`/posts/${post.id}`])`` returns cached data.
10​
11To run this example:
12​
13- `npm install`
14- `npm run dev`