首页 > 解决方案 > useLazyQuery 返回旧数据

问题描述

为什么该代码工作不正常?我更改了 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",

标签: reactjsreact-hooksreact-apollo

解决方案


您需要将 fetchPolicy 设置为:

fetchPolicy: 'network-only'

推荐阅读