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

README.md

18 lines1.9 KB

1# TanStack React Query Contacts Example (TanStack Router)
2​
3A contacts app whose router loaders read from the query cache and whose mutations invalidate it.
4​
5Built with [TanStack Router](https://tanstack.com/router).
6​
7## How it works
8​
9- `src/services/contacts/queries.ts` defines `contactsQueries.list(q)` and `contactsQueries.detail(id)` with `queryOptions`, and `contactsMutations.create()`, `update()`, and `delete()` with `mutationOptions`. The detail `queryFn` throws TanStack Router's `notFound()` when the contact does not exist. `src/services/contacts/api.ts` stores the contacts in `localStorage` behind a random delay.
10- The loaders in `src/routes/__root.tsx` (with `loaderDeps` on the `q` search param) and `src/routes/contacts.$contactId.route.tsx` call `queryClient.query({ ...options, staleTime: 'static' })` with the `queryClient` from the router context, which uses cached data when it exists and fetches only on a cache miss. Components read the same options with `useSuspenseQuery`, After the 10-second `staleTime` set in `src/index.tsx`, the data is stale, and `useSuspenseQuery` refetches it in the background on the next trigger, such as a component mount or window focus.
11- `src/index.tsx` creates the router with `defaultPreload: 'intent'`, so hovering a contact link runs its loader ahead of time.
12- `src/routes/__root.tsx` debounces the search input with `useDebouncedCallback` from TanStack Pacer and shows a spinner while `useIsFetching({ queryKey: contactsQueries.lists() })` is greater than 0.
13- `src/routes/contacts.new.tsx`, `src/routes/contacts.$contactId.edit.tsx`, and `src/routes/contacts.$contactId.index.tsx` run the mutations with `useMutation` and call `queryClient.invalidateQueries` afterward. The favorite button shows the pending value from `updateContact.variables` while the mutation `isPending`.
14​
15To run this example:
16​
17- `npm install`
18- `npm run dev`