无法读取未定义的 属性 'refetch'
Cannot read property 'refetch' of undefined
这是我的代码
const NewVerificationCode = () => {
const { loading, error, data = {}, refetch } = useQuery(CONFIRMATION_CODE, {
skip: true,
notifyOnNetworkStatusChange: true,
fetchPolicy: "no-cache",
});
return <div onClick={() => refetch()}>CONFIRMATION</div>;
};
const CONFIRMATION_CODE = gql`
query {
my {
sendNewTokenForConfirmation
}
}
`;
当我发出请求时出现错误
Uncaught TypeError: Cannot read property 'refetch' of undefined
data = {}
看起来你试图从未定义的 data
错误中 'escape' - 不推荐的方法...使用
if(loading) return <Loading />
...在主要内容之前或
if(data) return <div onClick={() => refetch()}>CONFIRMATION</div>;
return null;
skip
选项阻止钩子执行,所以不可能有 refetch
。
只需使用 useLazyQuery
或使用一些状态来控制 skip
选项,例如:
const [blocked, setBlocked] = useState(true);
const { loading, error, data = {}, refetch } = useQuery(CONFIRMATION_CODE, {
skip: blocked,
notifyOnNetworkStatusChange: true,
fetchPolicy: "no-cache",
});
if(data) return <div onClick={() => {if(blocked) setBlocked(false) else refetch()}}>CONFIRMATION ... {data.someValue}</div>;
return null;
};
这是我的代码
const NewVerificationCode = () => {
const { loading, error, data = {}, refetch } = useQuery(CONFIRMATION_CODE, {
skip: true,
notifyOnNetworkStatusChange: true,
fetchPolicy: "no-cache",
});
return <div onClick={() => refetch()}>CONFIRMATION</div>;
};
const CONFIRMATION_CODE = gql`
query {
my {
sendNewTokenForConfirmation
}
}
`;
当我发出请求时出现错误
Uncaught TypeError: Cannot read property 'refetch' of undefined
data = {}
看起来你试图从未定义的 data
错误中 'escape' - 不推荐的方法...使用
if(loading) return <Loading />
...在主要内容之前或
if(data) return <div onClick={() => refetch()}>CONFIRMATION</div>;
return null;
skip
选项阻止钩子执行,所以不可能有 refetch
。
只需使用 useLazyQuery
或使用一些状态来控制 skip
选项,例如:
const [blocked, setBlocked] = useState(true);
const { loading, error, data = {}, refetch } = useQuery(CONFIRMATION_CODE, {
skip: blocked,
notifyOnNetworkStatusChange: true,
fetchPolicy: "no-cache",
});
if(data) return <div onClick={() => {if(blocked) setBlocked(false) else refetch()}}>CONFIRMATION ... {data.someValue}</div>;
return null;
};