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
Copy file name to clipboardExpand all lines: content/docs/code-splitting.md
+46Lines changed: 46 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -139,6 +139,52 @@ function MyComponent() {
139
139
}
140
140
```
141
141
142
+
### Avoiding fallbacks
143
+
Any component may suspend as a result of rendering, even components that were already shown to the user. In order for screen content to always be consistent, if an already shown component suspends, React has to hide its tree up to the closest `<Suspense>` boundary. However, from the user's perspective, this can be disorienting.
In this example, if tab gets changed from `'photos'` to `'comments'`, but `Comments` suspends, the user will see a glimmer. This makes sense because the user no longer wants to see `Photos`, the `Comments` component is not ready to render anything, and React needs to keep the user experience consistent, so it has no choice but to show the `Glimmer` above.
175
+
176
+
However, sometimes this user experience is not desirable. In particular, it is sometimes better to show the "old" UI while the new UI is being prepared. You can use the new [`startTransition`](/docs/react-api.html#starttransition) API to make React do this:
177
+
178
+
```js
179
+
functionhandleTabSelect(tab) {
180
+
startTransition(() => {
181
+
setTab(tab);
182
+
});
183
+
}
184
+
```
185
+
186
+
Here, you tell React that setting tab to `'comments'` is not an urgent update, but is a [transition](/docs/react-api.html#transitions) that may take some time. React will then keep the old UI in place and interactive, and will switch to showing `<Comments />` when it is ready. See [Transitions](/docs/react-api.html#transitions) for more info.
187
+
142
188
### Error boundaries {#error-boundaries}
143
189
144
190
If the other module fails to load (for example, due to network failure), it will trigger an error. You can handle these errors to show a nice user experience and manage recovery with [Error Boundaries](/docs/error-boundaries.html). Once you've created your Error Boundary, you can use it anywhere above your lazy components to display an error state when there's a network error.
Copy file name to clipboardExpand all lines: content/docs/reference-react.md
+6-2Lines changed: 6 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -344,7 +344,9 @@ Note that rendering `lazy` components requires that there's a `<React.Suspense>`
344
344
345
345
### `React.Suspense` {#reactsuspense}
346
346
347
-
`React.Suspense` lets you specify the loading indicator in case some components in the tree below it are not yet ready to render. Today, lazy loading components is the **only** use case supported by `<React.Suspense>`:
347
+
`React.Suspense` lets you specify the loading indicator in case some components in the tree below it are not yet ready to render. In the future we plan to let `Suspense` handle more scenarios such as data fetching. You can read about this in [our roadmap](/blog/2018/11/27/react-16-roadmap.html).
348
+
349
+
Today, lazy loading components is the **only** use case supported by `<React.Suspense>`:
348
350
349
351
```js
350
352
// This component is loaded dynamically
@@ -364,7 +366,9 @@ function MyComponent() {
364
366
365
367
It is documented in our [code splitting guide](/docs/code-splitting.html#reactlazy). Note that `lazy` components can be deep inside the `Suspense` tree -- it doesn't have to wrap every one ofthem. The best practice is to place `<Suspense>` where you want to see a loading indicator, but to use `lazy()` wherever you want to do code splitting.
366
368
367
-
While this is not supported today, in the future we plan to let`Suspense` handle more scenarios such as data fetching. You can read about thisin [our roadmap](/blog/2018/11/27/react-16-roadmap.html).
369
+
> Note
370
+
>
371
+
> For content that is already shown to the user, switching back to a loading indicator can be disorienting. It is sometimes better to show the "old"UIwhile the newUI is being prepared. Todothis, you can use the newtransition APIs [`startTransition`](#starttransition) and [`useTransition`](/docs/hooks-reference.html#usetransition) to mark updates as transitions and avoid unexpected fallbacks.
0 commit comments