代码之家  ›  专栏  ›  技术社区  ›  Sefa

在react查询中获取未定义的“isLoading”状态

  •  0
  • Sefa  · 技术社区  · 2 年前

    在我将react查询更新到最新版本后,在使用useMutation时,“isLoading”状态未定义。这是代码:

    const useAddUserNote = (owner: string) => {
      const queryClient = useQueryClient();
      const { mutate, error, data, isLoading } = useMutation({
        mutationFn: (note: CreateNoteData) => addUserNote(note),
        onSuccess: () => {
          queryClient.invalidateQueries(["allUserNotes", owner]);
        },
      });
    
      if (error instanceof Error) error;
    
      return { mutate, data, error, isLoading };
    };
    

    这是类型错误

    Property 'isLoading' does not exist on type 'UseMutationResult<any, Error, CreateNoteData, unknown>'.

    当我控制台记录“isLoading”时,它是未定义的

    1 回复  |  直到 2 年前
        1
  •  1
  •   argbo    2 年前

    查看您的版本的API参考资料。在 latest documentation , isLoading 不再存在--它似乎在 version 4 。

    这也是 explicitly called out in the upgrade guide from version 4 to version 5 。升级时一定要阅读这些内容!

    您应该更新代码以使用 isPending :

    const { mutate, error, data, isPending } = useMutation({