examples/vue/contacts-tanstack-router
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Vue 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, andcontactsMutations.create(),update()anddelete()withmutationOptions. The detail query throws TanStack Router'snotFound()when the contact does not exist.src/router.tspasses thequeryClientfromsrc/queryClient.tsinto the router context withdefaultPreload: 'intent'. The loaders insrc/routes/__root.tsandsrc/routes/contacts.$contactId.route.tscallqueryClient.querywithstaleTime: 'static', so the list and the contact are in the cache before the route renders.src/routes/__root.component.vuereads the list withuseQuery(() => contactsQueries.list(q.value)), debounces typing withuseDebouncerfrom@tanstack/vue-pacerbefore updating theqsearch param, and shows a searching state fromuseIsFetching.src/routes/contacts.new.component.vue,src/routes/contacts.$contactId.edit.component.vue,src/routes/contacts.$contactId.index.component.vueandsrc/FavoriteButton.vuespreadcontactsMutationsintouseMutationand invalidatecontactsQueries.lists()orcontactsQueries.all().src/FavoriteButton.vueshows the pendingvariablesas the favorite state until the mutation settles.src/services/contacts/api.tsstores contacts inlocalStorageand waits a random time up to 800 ms on each call to simulate the network.
To run this example:
npm installnpm run dev