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

README.md

18 lines1.2 KB

1# TanStack Solid Query Optimistic Updates Example (SolidStart)
2​
3Adds todos optimistically in two ways: from the pending mutation's `variables`, or by writing to the cache in `onMutate` and rolling back on error.
4​
5Built with [SolidStart](https://start.solidjs.com).
6​
7## How it works
8​
9- `src/services/todos/queries.ts` defines `todosQueries.list()` with `queryOptions`, keyed by `['todos', 'list']`, and `todosMutations.create()` with `mutationOptions`.
10- `src/services/todos/api.ts` keeps the todos in server memory behind SolidStart server functions marked with `'use server'`. `createTodo` waits 500 ms and fails about 30% of the time.
11- `src/components/TodoListUI.tsx` renders the pending todo from `addTodoMutation.variables` while `isPending` is `true` and leaves the cache alone. `onSettled` invalidates the list.
12- `src/components/TodoListCache.tsx` cancels the list query and writes an optimistic todo into the cache with `queryClient.setQueryData` in `onMutate`, restores the snapshot in `onError`, and invalidates the list in `onSettled`.
13- `src/components/ApproachTabs.tsx` switches between the two approaches, and `src/routes/index.tsx` wraps them in `<Suspense>` and an `ErrorBoundary`.
14​
15To run this example:
16​
17- `npm install`
18- `npm run dev`