diff --git a/content/docs/code-splitting.md b/content/docs/code-splitting.md index 5ee4edddf36..eb09f1ba4d5 100644 --- a/content/docs/code-splitting.md +++ b/content/docs/code-splitting.md @@ -139,6 +139,52 @@ function MyComponent() { } ``` +### Avoiding fallbacks +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. + +Consider this tab switcher: + +```js +import React, { Suspense } from 'react'; +import Tabs from './Tabs'; +import Glimmer from './Glimmer'; + +const Comments = React.lazy(() => import('./Comments')); +const Photos = React.lazy(() => import('./Photos')); + +function MyComponent() { + const [tab, setTab] = React.useState('photos'); + + function handleTabSelect(tab) { + setTab(tab); + }; + + return ( + <div> + <Tabs onTabSelect={handleTabSelect} /> + <Suspense fallback={<Glimmer />}> + {tab === 'photos' ? <Photos /> : <Comments />} + </Suspense> + </div> + ); +} + +``` + +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. + +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: + +```js +function handleTabSelect(tab) { + startTransition(() => { + setTab(tab); + }); +} +``` + +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. + ### Error boundaries {#error-boundaries} 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. diff --git a/content/docs/reference-react.md b/content/docs/reference-react.md index f2c85cf7d61..6d0101549fc 100644 --- a/content/docs/reference-react.md +++ b/content/docs/reference-react.md @@ -344,7 +344,9 @@ Note that rendering `lazy` components requires that there's a `<React.Suspense>` ### `React.Suspense` {#reactsuspense} -`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>`: +`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). + +Today, lazy loading components is the **only** use case supported by `<React.Suspense>`: ```js // This component is loaded dynamically @@ -364,7 +366,9 @@ function MyComponent() { 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 of them. 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. -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 this in [our roadmap](/blog/2018/11/27/react-16-roadmap.html). +> Note +> +> 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" UI while the new UI is being prepared. To do this, you can use the new transition APIs [`startTransition`](#starttransition) and [`useTransition`](/docs/hooks-reference.html#usetransition) to mark updates as transitions and avoid unexpected fallbacks. ### `React.startTransition` {#starttransition}