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
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update the deferred guide to mention adding a key to the suspense bou…
…ndary
  • Loading branch information
henryStelle committed Mar 9, 2024
commit 6bd68c661639785d92365feb869aabd859fd406c
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