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

README.md

17 lines1.5 KB

1# TanStack React Query Contacts Example (React Router)
2​
3A contacts app whose router loaders read from the query cache and whose mutations invalidate it.
4​
5Built with [React Router](https://reactrouter.com).
6​
7## How it works
8​
9- `src/services/contacts/queries.ts` defines `contactsQueries.list(q)` and `contactsQueries.detail(id)` with `queryOptions`, keyed under `['contacts', 'list']` and `['contacts', 'detail']`. The detail `queryFn` throws a 404 `Response` 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` and `src/routes/contact.tsx` await `queryClient.query({ ...options, staleTime: 'static' })`, which uses cached data when it exists and fetches only on a cache miss. The route components then read the same options with `useSuspenseQuery`, After the 10-second `staleTime` set on the `QueryClient` 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/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.
12- The route actions in `src/routes/new.tsx`, `src/routes/edit.tsx`, `src/routes/contact.tsx`, and `src/routes/destroy.tsx` call the API, then `queryClient.invalidateQueries` so the list and detail refetch.
13​
14To run this example:
15​
16- `npm install`
17- `npm run dev`