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

README.md

14 lines798 Bytes

1# TanStack Solid 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` plus the first element of the `queryKey`, and sets it as `defaultOptions.queries.queryFn` on the `QueryClient`.
8- The `Posts` component calls `useQuery` with only `queryKey: ['/posts']`, and the `Post` component with ``queryKey: [`/posts/${props.postId}`]`` and `enabled: !!props.postId`. Neither passes a `queryFn`.
9- The `Posts` list calls ``queryClient.getQueryData([`/posts/${post.id}`])`` to style links whose post is already cached.
10​
11To run this example:
12​
13- `npm install`
14- `npm run dev`