Updating profile is working fine, but optimistic update is not working properly. An error occured 'old is not iterable'. How to fix this ?
export const useUpdateProfile = () => {
const queryClient = useQueryClient();
return useMutation(updateProfile, {
onMutate: async (newProfile) => {
// Cancel any outgoing refetches (so they don't overwrite our optimistic update)
await queryClient.cancelQueries("user-profile");
// Snapshot the previous value
const previousProfile = queryClient.getQueryData("user-profile");
// Optimistically update to the new value
queryClient.setQueryData(["user-profile"], (old) => [...old, newProfile]);
// Return a context object with the snapshotted value
return { previousTodos };
},
// If the mutation fails, use the context returned from onMutate to roll back
onError: (err, newTodo, context) => {
queryClient.setQueryData("user-profile", context.previousProfile);
},
// Always refetch after error or success:
onSettled: () => {
queryClient.invalidateQueries("user-profile");
},
});
};