【问题标题】:useLazyQuery returns old datauseLazyQuery 返回旧数据
【发布时间】:2020-07-05 22:17:23
【问题描述】:

为什么该代码工作不正常?我更改了 profileId 并没有发生任何事情,useLazyQuery 仍然返回旧数据。

    const [profile, setProfile] = useState<Profile>(null);
    const [getProfile, { loading, error, data }] = useLazyQuery<Data<'profile', Profile>, { id: string }>(PROFILE, {
      ssr: false,
    });

    useEffect(() => {
      getProfile({
        variables: { id: profileId },
      });
    }, [getProfile, profileId]);

    useEffect(() => {
      if (!loading && !error && data) {
        setProfile(data.profile);
      }
    }, [setProfile, data, loading, error]);

我正在使用:

    "@apollo/react-hooks": "^3.1.3",
    "apollo-client": "^2.6.8",

【问题讨论】:

    标签: reactjs react-hooks react-apollo


    【解决方案1】:

    您需要将 fetchPolicy 设置为:

    fetchPolicy: 'network-only'
    

    【讨论】:

      猜你喜欢
      • 2011-02-13
      • 2021-11-12
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2012-01-31
      相关资源
      最近更新 更多