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

README.md

14 lines886 Bytes

1# TanStack Preact Query Basic Example
2​
3A 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`.
4​
5## How it works
6​
7- `src/services/posts/queries.ts` defines `postsQueries.list()` and `postsQueries.detail(id)`, `queryOptions` factories keyed by `['posts', 'list']` and `['posts', 'detail', id]`.
8- In `src/index.tsx`, `Posts` and `Post` pass them to `useQuery`. `Posts` calls `queryClient.getQueryData(postsQueries.detail(post.id).queryKey)` to show posts that are already cached in bold green.
9- In `src/index.tsx`, the `QueryClient` sets `gcTime` to 24 hours, and `PersistQueryClientProvider` saves the cache with a `createAsyncStoragePersister` backed by `window.localStorage`, so visited posts load from the cache after a reload.
10​
11To run this example:
12​
13- `npm install`
14- `npm run dev`