useMutation invalidation fails

const { mutate: updateUserData } = trpc.userInfo.updateUserInfo.useMutation({
    async onMutate(newUserData) {
      // Cancel any outgoing refetches (so they don't overwrite our optimistic update)
      utils.userInfo.getCurrentUser.cancel();
      const previousUserData = utils.userInfo.getCurrentUser.getData();
      // Optimistic update;
      utils.userInfo.getCurrentUser.setData(undefined, newUserData);
      return { previousUserData };
    },
    onError(_, __, context) {
      utils.userInfo.getCurrentUser.setData(
        undefined,
        context?.previousUserData
      );
      toast.error("An error occured while updating your data", {
        position: toast.POSITION.BOTTOM_RIGHT,
      });
    },
    // Always refetch after error or success - sync the cache no matter what
    onSettled() {
      console.log("invalidateQueries");
      utils.userInfo.getCurrentUser.invalidate();
    },
  });
Was this page helpful?