Skip to content
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

Update docs to mention fix for case when Await fallback not rendering #11335

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
- holynewbie
- hongji00
- hsbtr
- henryStelle
- hyesungoh
- ianflynnwork
- IbraRouisDev
Expand Down
6 changes: 4 additions & 2 deletions docs/components/await.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function Book() {
<div>
<h1>{book.title}</h1>
<p>{book.description}</p>
<React.Suspense fallback={<ReviewsSkeleton />}>
<React.Suspense key={book.id} fallback={<ReviewsSkeleton />}>
<Await
resolve={reviews}
errorElement={
Expand All @@ -34,6 +34,8 @@ function Book() {

**Note:** `<Await>` expects to be rendered inside of a `<React.Suspense>` or `<React.SuspenseList>` parent to enable the fallback UI.

**Note:** Add a data-bound `key` to the parent of `<Await>` to ensure if the route changes, the fallback is invoked while new data is loaded.

## Type declaration

```tsx
Expand Down Expand Up @@ -133,7 +135,7 @@ function Book() {
<div>
<h1>{book.title}</h1>
<p>{book.description}</p>
<React.Suspense fallback={<ReviewsSkeleton />}>
<React.Suspense key={book.id} fallback={<ReviewsSkeleton />}>
<Await
// and is the promise we pass to Await
resolve={reviews}
Expand Down
5 changes: 4 additions & 1 deletion docs/guides/deferred.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ async function loader({ params }) {

return defer({
packageLocation: packageLocationPromise,
id: params.packageId,
});
}

Expand All @@ -90,6 +91,7 @@ export default function PackageRoute() {
<main>
<h1>Let's locate your package</h1>
<React.Suspense
key={data.id}
fallback={<p>Loading package location...</p>}
>
<Await
Expand Down Expand Up @@ -124,6 +126,7 @@ export default function PackageRoute() {
<main>
<h1>Let's locate your package</h1>
<React.Suspense
key={data.id}
fallback={<p>Loading package location...</p>}
>
<Await
Expand Down Expand Up @@ -199,7 +202,7 @@ It's all trade-offs, and what's neat about the API design is that it's well suit

### When does the `<Suspense/>` fallback render?

The `<Await />` component will only throw the promise up the `<Suspense>` boundary on the initial render of the `<Await />` component with an unsettled promise. It will not re-render the fallback if props change. Effectively, this means that you _will not_ get a fallback rendered when a user submits a form and loader data is revalidated. You _will_ get a fallback rendered when the user navigates to the same route with different params (in the context of our above example, if the user selects from a list of packages on the left to find their location on the right).
The `<Await />` component will only throw the promise up the `<Suspense>` boundary on the initial render of the `<Await />` component with an unsettled promise. It will not re-render the fallback if props change. Effectively, this means that you _will not_ get a fallback rendered when a user submits a form and loader data is revalidated. You _will_ get a fallback rendered when the user navigates to the same route with different params (in the context of our above example, if the user selects from a list of packages on the left to find their location on the right). If the fallback is not being rendered when the props change, ensure you have a `key` on the `<Suspense>` boundary to force it to recognize that the props have changed.

This may feel counter-intuitive at first, but stay with us, we really thought this through and it's important that it works this way. Let's imagine a world without the deferred API. For those scenarios you're probably going to want to implement Optimistic UI for form submissions/revalidation.

Expand Down