examples/lit/optimistic-updates
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Lit Query Optimistic Updates Example#
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.
How it works#
src/services/todos/queries.tsdefinestodosQueries.list(), aqueryOptionsfactory keyed by['todos', 'list'], andtodosMutations.create()withmutationOptions.src/todo-list-ui.tsreads the list withcreateQueryControllerand renders the pending todo from the mutation'svariableswhileisPendingistrue, without touching the cache.onSettledinvalidates the list.src/todo-list-cache.tsusesonMutateon itscreateMutationControllerto callcancelQueries, save the current list withgetQueryData, and add a todo with anoptimistic-id throughsetQueryData.onErrorrestores the saved list, andonSettledinvalidates it.src/approach-tabs.tsswitches between the two elements.src/mocks.tsis an MSW handler for/api/todos. The POST handler fails about 30% of the time with a 500 so the rollback can be seen.
To run this example:
npm installnpm run dev