website/docs/api-reference/hooks/use-lazy-load-query.mdx
import DocsRating from '@site/src/core/DocsRating';
useLazyLoadQueryHook used to fetch a GraphQL query during render. This hook can trigger multiple nested or waterfalling round trips if used without caution, and waits until render to start a data fetch (when it can usually start a lot sooner than render), thereby degrading performance. Instead, prefer usePreloadedQuery.
const React = require('React');
const {graphql, useLazyLoadQuery} = require('react-relay');
function App() {
const data = useLazyLoadQuery(
graphql`
query AppQuery($id: ID!) {
user(id: $id) {
name
}
}
`,
{id: 4},
{fetchPolicy: 'store-or-network'},
);
return <h1>{data.user?.name}</h1>;
}
gqlQuery: GraphQL query specified using a graphql template literal.variables: Object containing the variable values to fetch the query. These variables need to match GraphQL variables declared inside the query.options: [Optional] options object
fetchPolicy: [Optional] Determines if cached data should be used, and when to send a network request based on the cached data that is currently available in the Relay store (for more details, see our Fetch Policies and Garbage Collection guides):
fetchKey: [Optional] A fetchKey can be passed to force a re-evaluation of the current query and variables when the component re-renders, even if the variables didn't change, or even if the component isn't remounted (similarly to how passing a different key to a React component will cause it to remount). If the fetchKey is different from the one used in the previous render, the current query will be re-evaluated against the store, and it might be refetched depending on the current fetchPolicy and the state of the cache.networkCacheConfig: [Optional] Default value: {force: true}. Object containing cache config options for the network layer. Note that the network layer may contain an additional query response cache which will reuse network responses for identical queries. If you want to bypass this cache completely (which is the default behavior), pass {force: true} as the value for this option.UNSTABLE_renderPolicy: [Optional] Undocumented option.data: Object that contains data which has been read out from the Relay store; the object matches the shape of specified query.
data above is: {| user: ?{| name: ?string |} |}.import UseLazyLoadQueryExtra from './_use-lazy-load-query-extra.mdx';
<UseLazyLoadQueryExtra /> <DocsRating />