Skip to content

Commit a2293f3

Browse files
committed
docs: added note about defer usage w/ react query
1 parent f54a4c1 commit a2293f3

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/guides/data-libs.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,47 @@ export const action = async ({ request, params }) => {
6666
};
6767
```
6868

69+
## Usage with `defer`
70+
71+
You can similarly take advantage of the deferred APIs:
72+
73+
```jsx lines=[2,10,14,28]
74+
function loader() {
75+
return defer({
76+
// no await!
77+
someData: queryClient.fetchQuery("someKey", fn),
78+
});
79+
}
80+
81+
function Comp() {
82+
// *do* useLoaderData for promise
83+
const { someData } = useLoaderData();
84+
return (
85+
<div>
86+
<h1>Something</h1>
87+
<Await
88+
resolve={someData}
89+
errorElement={<div>Oops!</div>}
90+
>
91+
<SomeView />
92+
</Await>
93+
</div>
94+
);
95+
}
96+
97+
function SomeView() {
98+
// instead of accessing with useAsyncValue
99+
// const someData = useAsyncValue();
100+
// `useQuery` as usual
101+
const { data } = useQuery("someKey");
102+
// ...
103+
}
104+
```
105+
106+
## The Overlap
107+
108+
Hooks like `useQuery` often return pending and error states you can use to branch your UI. With React Router, you can keep all of that branching out of your happy path components and rely on [`errorElement`][errorelement], [`useNavigation`][usenavigation], and [`Await`][await] instead.
109+
69110
## Conclusion
70111

71112
With all of these APIs working together, you can now use [`useNavigation`][usenavigation] from React Router to build pending states, optimistic UI, and more. Use React Router for timing of data loading, mutations, and navigation state, then use libraries like React Query for the actual implementation of loading, invalidating, storage, and caching.
@@ -76,3 +117,4 @@ With all of these APIs working together, you can now use [`useNavigation`][usena
76117
[action]: ../route/action
77118
[tkdodo]: https://tkdodo.eu/blog/react-query-meets-react-router
78119
[usenavigation]: ../hooks/use-navigation
120+
[await]: ../components/await

0 commit comments

Comments
 (0)