如何跳过`useLazyLoadQuery`中的请求

问题描述 投票:0回答:1

如何跳过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
react-hooks relayjs
1个回答
0
投票

您可以使用@skip or @include directive。如果根据指令条件查询最终为空,则不会发出网络请求。考虑示例:

@skip

@include产生一个空的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 />; } ,导致无网络请求

© www.soinside.com 2019 - 2024. All rights reserved.