Skip to content

Issue with Persisting Mutations in React-Native with React-Query #5248

Answered by m4rv4ul3r
sajadghawami asked this question in Q&A

You must be logged in to vote

Think I found a solution. You have to create a default Mutation, otherwise it is not persisted. See: https://tanstack.com/query/latest/docs/react/guides/mutations#persisting-offline-mutations

// Define default mutation
queryClient.setMutationDefaults(['create-item'], {
  mutationFn: async (data: CreateItemData) => {
    return createItem(data);
  },
  onSettled: (_data, _err) => {
    console.log('settled item');
  },
});

Then you can call the mutation by it mutationKey. Important: If you overwrite functions like mutationFn or onSettled it's not working.

// Call the default mutation
const createItem = useMutation({ mutationKey: ['create-item'] });
createItem.mutate(data)

Replies: 3 comments 10 replies

You must be logged in to vote
4 replies
@sajadghawami

@trymbill

@sajadghawami

@m4rv4ul3r

You must be logged in to vote
3 replies
@TkDodo

@dberardo-com

@TkDodo

Answer selected by TkDodo

You must be logged in to vote
3 replies
@sajadghawami

@dberardo-com

@AlshehriAli0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
7 participants
Converted from issue

This discussion was converted from issue #5244 on April 10, 2023 09:43.