You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
See the [API reference](../api/react/hooks#usefragment) for more details on the supported options.
591
591
592
+
<MinVersion version="3.13.0">
593
+
## `useSuspenseFragment`
594
+
</MinVersion>
595
+
596
+
For those that have integrated with React [Suspense](https://react.dev/reference/react/Suspense), `useSuspenseFragment` is available as a drop-in replacement for `useFragment`. `useSuspenseFragment` works identically to `useFragment` but will suspend while `data` is incomplete.
597
+
598
+
Let's update the example from the previous section to use `useSuspenseFragment`. First, we'll update our `Item` component and replace `useFragment` with `useSuspenseFragment`. Since we are using Suspense, we no longer have to check for a `complete` property to determine if the result is complete because the component will suspend otherwise.
Next, we'll will wrap our `Item` components in a `Suspense` boundary to show a loading indicator if the data from `ItemFragment` is not complete. Since we're using Suspense, we'll replace `useQuery` with `useSuspenseQuery` as well:
615
+
616
+
```tsx
617
+
functionList() {
618
+
const { data } =useSuspenseQuery(listQuery);
619
+
620
+
return (
621
+
<ol>
622
+
{data.list.map(item=> (
623
+
<Suspense fallback={<Spinner />}>
624
+
<Item key={item.id} item={item}/>
625
+
</Suspense>
626
+
))}
627
+
</ol>
628
+
);
629
+
}
630
+
```
631
+
632
+
And that's it! Suspense made our `Item` component a bit more succinct since we no longer need to check the `complete` property to determine if we can safely use `data`.
633
+
634
+
<Note>
635
+
In most cases, `useSuspenseFragment` will not suspend when rendered as a child of a query component. In this example `useSuspenseQuery` loads the full query data before each `Item` is rendered so the `data` inside each fragment is already complete. The `Suspense` boundary in this example ensures that a loading spinner is shown if field data is removed for any given item in the list in the cache, such as when a manual cache update is performed.
636
+
</Note>
637
+
638
+
### Using `useSuspenseFragment` with `@defer`
639
+
640
+
`useSuspenseFragment` is helpful when combined with the [`@defer` directive](./directives#defer) to show a loading state while the fragment data is streamed to the query. Let's update our `GetItemList` query to defer loading the `ItemFragment`'s fields.
641
+
642
+
```graphql
643
+
query GetItemList {
644
+
list {
645
+
id
646
+
...ItemFragment @defer
647
+
}
648
+
}
649
+
```
650
+
651
+
Our list will now render as soon as our list returns but before the data for `ItemFragment` is loaded.
652
+
653
+
<Caution>
654
+
You **must** ensure that any key fields used to identify the object passed to the `from` option are not deferred. If they are, you risk suspending the `useSuspenseFragment` hook forever. If you need to defer loading key fields, conditionally render the component until the object passed to the `from` option is identifiable by the cache.
0 commit comments