首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >响应查询乐观更新“旧不可重复”为什么会出现此错误

响应查询乐观更新“旧不可重复”为什么会出现此错误
EN

Stack Overflow用户
提问于 2022-07-25 22:52:36
回答 1查看 239关注 0票数 1

更新配置文件运行良好,但乐观更新不能正常工作。发生的错误“旧不可迭代”。怎么解决这个问题?

代码语言:javascript
复制
  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");
    },
  });
};
EN

回答 1

Stack Overflow用户

发布于 2022-07-27 16:44:09

似乎获取user-profile会给您返回undefined,可能是因为它不存在吗?在传播之前,一定要检查undefined

代码语言:javascript
复制
// Optimistically update to the new value
queryClient.setQueryData(["user-profile"], (old) => old ? [...old, newProfile] : []);

如果您已经升级到v4 (@tan堆栈/ the query),还可以从更新程序返回未定义的内容以退出更新:

代码语言:javascript
复制
queryClient.setQueryData(["user-profile"], (old) => old ? [...old, newProfile] : undefined);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73116110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档