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

README.md

15 lines1.0 KB

1# TanStack React 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)` with `queryOptions`, keyed under `['posts']`. `src/services/posts/api.ts` fetches the posts from JSONPlaceholder.
8- `src/index.tsx` renders `Posts` with `useQuery(postsQueries.list())` and `Post` with `useQuery(postsQueries.detail(postId))`. Both show "Background Updating..." while `isFetching` is `true`.
9- In `src/index.tsx`, the list marks a post as visited when `queryClient.getQueryData(postsQueries.detail(post.id).queryKey)` returns cached data.
10- `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.
11​
12To run this example:
13​
14- `npm install`
15- `npm run dev`