使用 Apollo Client 在 useMutation 之后未触发 useEffect

useEffect not triggered after useMutation using Apollo Client

我有一个父组件有:

  const { loading, error, data } = useQuery(GET_DATA, {
    notifyOnNetworkStatusChange: true
  });

  // Handle data
  useEffect(() => {
    if (loading || error) return;
    setZendeskData(data);
  }, [loading, error, data]);

然后在子组件中我有:

  const [createItem] = useMutation(CREATE_SESSION, {
    refetchQueries: [{ query: GET_DATA }]
  });

当调用createItem 并运行refetchQuery 时,父组件中的useEffect 没有更新数据。

确保数据始终是最新的最佳方法是什么?

看看 notifyOnNetworkStatusChange 来自 docs:

Let's return to our refetching example from the previous section. If you click the refetch button, you'll see that the component doesn't re-render until the new data arrives. What if we want to indicate to the user that we're refetching the photo?

我认为它与您的确切用例有关。然后,您将从 useQuery 添加 networkStatus 作为 useEffect 依赖项。