Skip to content

Commit 17ab5ee

Browse files
KyleAMathewsclaude
andauthored
docs: add navigation workflow pattern for optimistic: false mutations (#588)
Co-authored-by: Claude <[email protected]>
1 parent de14bb0 commit 17ab5ee

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

docs/overview.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,44 @@ tx.mutate(() => {
880880
})
881881
```
882882

883+
##### Common workflow: Wait for persistence before navigation
884+
885+
A common pattern with `optimistic: false` is to wait for the mutation to complete before navigating to a new page or showing success feedback:
886+
887+
```typescript
888+
const handleCreatePost = async (postData) => {
889+
// Insert without optimistic updates
890+
const tx = postsCollection.insert(postData, { optimistic: false })
891+
892+
try {
893+
// Wait for write to server and sync back to complete
894+
await tx.isPersisted.promise
895+
896+
// Server write and sync back were successful - safe to navigate
897+
navigate(`/posts/${postData.id}`)
898+
} catch (error) {
899+
// Show error toast or notification
900+
toast.error('Failed to create post: ' + error.message)
901+
}
902+
}
903+
904+
// Works with updates and deletes too
905+
const handleUpdateTodo = async (todoId, changes) => {
906+
const tx = todoCollection.update(
907+
todoId,
908+
{ optimistic: false },
909+
(draft) => Object.assign(draft, changes)
910+
)
911+
912+
try {
913+
await tx.isPersisted.promise
914+
navigate('/todos')
915+
} catch (error) {
916+
toast.error('Failed to update todo: ' + error.message)
917+
}
918+
}
919+
```
920+
883921
## Usage examples
884922

885923
Here we illustrate two common ways of using TanStack DB:

0 commit comments

Comments
 (0)