-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document new React APIs in 3.9 - Part 1 of 2 #11512
Changes from 1 commit
c4f727c
08eb80e
8a427f0
fe9674a
fefd6a4
ebe8700
908a1e7
28ab24d
431e55c
4434d90
2499789
bb96e55
e5d6c6f
a7cf933
23c54cc
3ab6262
e2e51e1
173c1a4
4c6877d
b3c64bb
958d6ba
7be17c1
4cdcb5b
b1e423a
d7db9a9
76db390
efa6f23
8577574
53a08ee
974a59f
2c556f1
5a40df9
892db6a
a754c6c
41c794b
97bb5ba
ea04c9e
9df38c6
0238b0d
974133b
4c541ac
0f9f34a
87ed45a
aa7c5d9
4a4b1ba
fdcd394
864ff7f
3503883
f701bb6
7343d6c
680714d
d457cad
f3d9fde
f7c1de1
08c845d
4176bfe
b1c44cf
c9a1c24
d6ff311
5966ad2
b76cd25
3207653
a135979
820742a
c236219
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -433,6 +433,59 @@ When the network request for `GET_DOG_QUERY` completes, the `Dog` component unsu | |
|
||
The `useBackgroundQuery` hook used in a parent component is responsible for kicking off fetches, but doesn't deal with reading or rendering data. This is delegated to the `useReadQuery` hook used in a child component. This separation of concerns provides a nice performance benefit because cache updates are observed by `useReadQuery` and re-render only the child component. You may find this as a useful tool to optimize your component structure to avoid unnecessarily re-rendering parent components when cache data changes. | ||
|
||
<MinVersion version="3.9.0"> | ||
|
||
### Querying in response to user interaction | ||
|
||
</MinVersion> | ||
|
||
`useSuspenseQuery` and `useBackgroundQuery` are useful hooks that will begin loading data as soon as the hook is mounted. But what happens when you'd like to start loading your query in response to a user interaction? With `useSuspenseQuery` and `useBackgroundQuery`, you'd need to update React state in response to the user interaction in order to re-render your component. | ||
jerelmiller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Starting with Apollo Client `3.9.0`, you can use `useLoadableQuery` to start loading data in response to user interaction. This is not only more ergonomic, but starts loading the query immediately, providing a nice performance benefit. | ||
phryneas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
`useLoadableQuery` returns a function that when called will begin fetching the query with the provided variables. Like `useBackgroundQuery`, you get access to a `queryRef` that is passed to `useReadQuery` to read the data in a child component. When the child component renders before the query has finished loading, the child component suspends. | ||
jerelmiller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Let's update our example to start loading the dog's details as a result of selecting from a dropdown. | ||
jerelmiller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```tsx | ||
function App() { | ||
const { data } = useSuspenseQuery(GET_DOGS_QUERY); | ||
const [queryRef, loadDog] = useLoadableQuery(GET_DOG_QUERY) | ||
jerelmiller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return ( | ||
<> | ||
<select | ||
onChange={(e) => loadDog({ id: e.target.value })} | ||
> | ||
{data.dogs.map(({ id, name }) => ( | ||
<option key={id} value={id}> | ||
{name} | ||
</option> | ||
))} | ||
</select> | ||
<Suspense fallback={<div>Loading...</div>}> | ||
{queryRef && <Dog queryRef={queryRef} />} | ||
</Suspense> | ||
</> | ||
); | ||
} | ||
|
||
function Dog({ queryRef }) { | ||
jerelmiller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const { data } = useReadQuery(queryRef) | ||
|
||
return ( | ||
<> | ||
<div>Name: {data.dog.name}</div> | ||
<div>Breed: {data.dog.breed}</div> | ||
</> | ||
); | ||
} | ||
``` | ||
|
||
> It's important to note that the `queryRef` is `null` until the query loading function is called for the first time. You should conditionally render the child component when the query has not yet been loaded. | ||
|
||
We begin fetching our `GET_DOG_QUERY` as soon as a new dog is selected rather than waiting for our `Dog` component to re-render. When the `Dog` component renders, it reads data from the `GET_DOG_QUERY`, and if not ready, will suspend. As a result of this change, we've also removed the need to track the selected dog ID as part of React state! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Again, I'm not sure if this should be the main selling angle here - that render is probably instant, and it will happen either way, so a data fetch would at best happen a handful of milliseconds sooner. I'd really focus more on the data flow. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Take 2 d7db9a9 |
||
|
||
### Refetching and pagination | ||
|
||
Apollo's Suspense data fetching hooks return functions for refetching query data via the `refetch` function, and fetching additional pages of data via the `fetchMore` function. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another point of feedback: Does this section include enough information?
Some other things we could talk about if we want to provide additional info:
variables
option and instead variables are passed to theloadQuery
function.reset
function that when called, sets thequeryRef
back tonull
This section is also provided right before pagination/refetching, which uses
useBackgroundQuery
as the example. Should this be moved afterward? Feedback welcome!