Issue with Persisting Mutations in React-Native with React-Query #5248
-
Describe the bugHey, I am facing an issue with persisting mutations in a React-Native app that uses React-Query and trpc. I want to be able to persist mutations even when the app is closed, so that when the app is started again, the mutations are tried again. I have searched for a guide or solution to this issue, but couldn't find anything. The current persist implementation only works when the app is running and has not been closed. When the app is closed and started again, all the mutation cache is lost, which does not seem like normal behavior. I have noticed that AsyncStorage persists the calls while the app is running, and when the app goes online again, all the cached mutations are called. The only issue seems to be when the app is closed. I am not sure if this is a trpc problem, since the case where the app is running works without a problem. I am looking for a solution to persist mutations even when the app is closed. Your minimal, reproducible exampleCan provide if needed. Steps to reproduce
Expected behaviorMutations should be persisted even when the app is closed, and should be tried again when the app is started again. Actual behaviour:Mutation cache is lost when the app is closed, and mutations are not tried again when the app is started again. How often does this bug happen?Every time Screenshots or VideosNo response Platform
Tanstack Query adapterreact-query TanStack Query version4.28.0 TypeScript versionNo response Additional contextThis is the current implementation of the wrapper: export const trpc = createTRPCReact<AppRouter>();
const asyncStoragePersister = createAsyncStoragePersister({
storage: AsyncStorage,
});
/**
* A wrapper for your app that provides the TRPC context.
* Use only in _app.tsx
*/
export const TRPCProvider: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const [queryClient] = React.useState(
() =>
new QueryClient({
defaultOptions: {
queries: {
cacheTime: Infinity,
retry: true,
},
mutations: {
cacheTime: Infinity,
retry: true,
},
},
})
);
const [trpcClient] = React.useState(() =>
trpc.createClient({
transformer: superjson,
links: [
httpBatchLink({
url: `${getBaseUrl()}/api/trpc`,
async headers() {
const token = await getData("token");
return { Authorization: `Bearer ${token}` };
},
}),
],
})
);
return (
<trpc.Provider client={trpcClient} queryClient={queryClient}>
<PersistQueryClientProvider
client={queryClient}
persistOptions={{
persister: asyncStoragePersister,
maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days
}}
onSuccess={() => {
queryClient.resumePausedMutations().then(() => {
queryClient.invalidateQueries();
});
}}
>
{children}
</PersistQueryClientProvider>
</trpc.Provider>
);
}; Thanks in advance for any help or guidance on this issue. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
I think we've discussed this on discord. You have to narrow down what the problem is. Could be trpc, or AsyncStorage, or react-query. |
Beta Was this translation helpful? Give feedback.
-
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) |
Beta Was this translation helpful? Give feedback.
-
@sajadghawami have you found any solution |
Beta Was this translation helpful? Give feedback.
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
Then you can call the mutation by it mutationKey. Important: If you overwrite functions like mutationFn or onSettled it's not working.