Open History to see the last commit
TanStack Solid Query Contacts Example (TanStack Router)#
A contacts app whose router loaders read from the query cache and whose mutations invalidate it.
Built with TanStack Router.
How it works#
src/services/contacts/queries.tsdefinescontactsQueries.list(q)andcontactsQueries.detail(id)withqueryOptions, pluscontactsMutations.create(),update()anddelete()withmutationOptions. The detailqueryFnthrows TanStack Router'snotFound()when the contact does not exist.src/services/contacts/api.tsstores the contacts inlocalStoragebehind a random fake network delay.src/index.tsxcreates theQueryClientwith a 10-secondstaleTimeand passes it to the router ascontext.- The route
loaders insrc/routes/__root.tsxandsrc/routes/contacts.$contactId.route.tsxcallqueryClient.query(...)withstaleTime: 'static', so navigation waits for cached data without refetching it. The components read the same options withuseQuery. src/routes/__root.tsxdebounces the search input into theqsearch param withcreateDebouncerfrom TanStack Pacer and shows a spinner whileuseIsFetchingcounts a fetching list query.src/routes/contacts.new.tsx,src/routes/contacts.$contactId.edit.tsxandsrc/routes/contacts.$contactId.index.tsxrun the mutations withuseMutationand callqueryClient.invalidateQueriesafterwards. The favorite button shows the new value from the pending mutation'svariables.
To run this example:
npm installnpm run dev