Skip to content

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

Answered by m4rv4ul3r
sajadghawami asked this question in Q&A
Discussion options

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 7 replies

Comment options

You must be logged in to vote
4 replies
@sajadghawami
Comment options

@trymbill
Comment options

@sajadghawami
Comment options

@m4rv4ul3r
Comment options

Comment options

You must be logged in to vote
1 reply
@TkDodo
Comment options

Answer selected by TkDodo
Comment options

You must be logged in to vote
2 replies
@sajadghawami
Comment options

@miteshdevg
Comment options

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

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