reactjs - 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",
解决方案
您需要将 fetchPolicy 设置为:
fetchPolicy: 'network-only'
推荐阅读
- jquery - 当返回是某个值时如何启动一个动作
- java - 如何在不使用 Comparator 的情况下按在 Java 中遇到子字符串的次数对对象列表进行排序?
- android - 当密钥是在 Android 密钥库中支持的保险箱时无法验证签名
- node.js - 我们有一个文化信息类。net ,我们在 node js 中是否有类似的结构/包?
- npm - npm install 命令在每一行给出 ERR
- database - 表之间有什么关系?
- c# - Winforms Listbox,尽管Drawmode被OwnerDrawFixed,如何使显示成员工作?
- python-3.x - 函数在本机工作,但在从另一个页面调用时不起作用
- c - 在c中反转字符串后得到垃圾
- css - 如何设置 mvc 局部视图,使其不受布局页面的 css 影响?