examples/solid/optimistic-updates-solid-start2
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Solid Query Optimistic Updates Example (SolidStart)#
Adds todos optimistically in two ways: from the pending mutation's variables, or by writing to the cache in onMutate and rolling back on error.
Built with SolidStart.
How it works#
src/services/todos/queries.tsdefinestodosQueries.list()withqueryOptions, keyed by['todos', 'list'], andtodosMutations.create()withmutationOptions.src/services/todos/api.tskeeps the todos in server memory behind SolidStart server functions marked with'use server'.createTodowaits 500 ms and fails about 30% of the time.src/components/TodoListUI.tsxrenders the pending todo fromaddTodoMutation.variableswhileisPendingistrueand leaves the cache alone.onSettledinvalidates the list.src/components/TodoListCache.tsxcancels the list query and writes an optimistic todo into the cache withqueryClient.setQueryDatainonMutate, restores the snapshot inonError, and invalidates the list inonSettled.src/components/ApproachTabs.tsxswitches between the two approaches, andsrc/routes/index.tsxwraps them in<Suspense>and anErrorBoundary.
To run this example:
npm installnpm run dev