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

README.md

18 lines1.4 KB

1# TanStack Solid Query Contacts Example (Solid Router)
2​
3A contacts app whose router loaders read from the query cache and whose mutations invalidate it.
4​
5Built with [Solid Router](https://docs.solidjs.com/solid-router).
6​
7## How it works
8​
9- `src/services/contacts/queries.ts` defines `contactsQueries.list(q)` and `contactsQueries.detail(id)` with `queryOptions`, keyed by `['contacts', 'list', { q }]` and `['contacts', 'detail', id]`. The detail `queryFn` throws "Not Found" when the contact does not exist.
10- `src/services/contacts/api.ts` stores the contacts in `localStorage` behind a random fake network delay. `src/queryClient.ts` creates the shared `QueryClient` with a 10-second `staleTime`.
11- `src/index.tsx` registers each route's `preload` function. `src/routes/root.tsx` and `src/routes/contact.tsx` start the fetch there with `queryClient.query(...)`, and the components read the same options with `useQuery`.
12- `src/routes/root.tsx` debounces the search input into the `q` search param with `createDebouncer` from TanStack Pacer and shows a spinner while `useIsFetching` counts a fetching list query.
13- The Solid Router actions in `src/routes/new.tsx`, `src/routes/edit.tsx`, `src/routes/destroy.tsx` and `src/routes/contact.tsx` update `localStorage` and then call `queryClient.invalidateQueries` before redirecting.
14​
15To run this example:
16​
17- `npm install`
18- `npm run dev`