File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -880,6 +880,44 @@ tx.mutate(() => {
880
880
})
881
881
` ` `
882
882
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
+
883
921
## Usage examples
884
922
885
923
Here we illustrate two common ways of using TanStack DB :
You can’t perform that action at this time.
0 commit comments