Skip to content

Instantly share code, notes, and snippets.

@ilxanlar
Last active April 10, 2021 11:26
Show Gist options
  • Save ilxanlar/b98f6cb9b3dd387d8941a1fa4b468271 to your computer and use it in GitHub Desktop.
Save ilxanlar/b98f6cb9b3dd387d8941a1fa4b468271 to your computer and use it in GitHub Desktop.
useMutation(yourMutationFunction, {
// Before the mutation starts
async onMutate() {
/* 1. Stop the queries that may affect this operation */
/* 2. Modify cache to reflect this optimistic update */
/* 3. Return a snapshot so we can rollback in case of failure */
const context = { ... }
return context
},
// Mutation throws an error
onError(error, newItem, context) {
/* Rollback the changes using the snapshot */
},
// After mutation ends successfully
onSuccess() {
/* Refetch or invalidate related queries */
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment