如何在useLazyLoadQuery中跳过请求/查询。
import { useLazyLoadQuery } from 'react-relay/hooks';
const id = props.selectedId // id can be a number or null
const user = useLazyLoadQuery(query, {id}) // skip query/request network if id is null发布于 2020-03-16 16:11:15
您可以使用@skip or @include directive。如果查询在指令条件下为空,则不会发出网络请求。考虑一下这个例子:
import { useLazyLoadQuery } from 'react-relay/hooks';
function MaybeUser(props) {
const { userId } = props;
// is optional/nullable
useLazyLoadQuery(
graphql`
query MaybeUserQuery($userId: ID!, $skip: Boolean!) {
user(id: $userId) @skip(if: $skip) {
fullName
}
}
`,
{
userId: userId || '', // we need to pass something because of the query $userId type decleration
skip: !userId, // skip when there is no user ID
},
);
return <magic />;
}空userId生成一个空的MaybeUserQuery,导致没有网络请求。
https://stackoverflow.com/questions/60708205
复制相似问题