diff --git a/beta/src/content/apis/react/Suspense.md b/beta/src/content/apis/react/Suspense.md index 2cf96b72..614f56ef 100644 --- a/beta/src/content/apis/react/Suspense.md +++ b/beta/src/content/apis/react/Suspense.md @@ -95,11 +95,11 @@ The sequence will be: --- -### Lazy loading components with Suspense {/*suspense-for-code-splitting*/} +### Lazy-loading components with Suspense {/*lazy-loading-components-with-suspense*/} The [`lazy`](/apis/react/lazy) API is powered by Suspense. When you render a component imported with `lazy`, it will suspend if it hasn't loaded yet. This allows you to display a loading indicator while your component's code is loading. -```js {3,12-14} +```js {3,12-15} import { lazy, Suspense, useState } from 'react'; const MarkdownPreview = lazy(() => import('./MarkdownPreview.js')); @@ -112,6 +112,7 @@ function MarkdownEditor() { ... {showPreview && ( }> +

Preview

)} @@ -143,6 +144,7 @@ export default function MarkdownEditor() {
{showPreview && ( }> +

Preview

)} diff --git a/beta/src/content/apis/react/lazy.md b/beta/src/content/apis/react/lazy.md index 39cb21bf..41bdd893 100644 --- a/beta/src/content/apis/react/lazy.md +++ b/beta/src/content/apis/react/lazy.md @@ -2,22 +2,206 @@ title: lazy --- - + -This section is incomplete, please see the old docs for [lazy.](https://reactjs.org/docs/react-api.html#reactlazy) +`lazy` lets you defer loading component's code until it is rendered for the first time. - +```js +const SomeComponent = lazy(load) +``` + - + + +--- + +## Usage {/*usage*/} + +### Lazy-loading components with Suspense {/*suspense-for-code-splitting*/} -`React.lazy()` lets you define a component that is loaded dynamically. This helps reduce the bundle size to delay loading components that aren’t used during the initial render. +Usually, you import components with the static [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) declaration: ```js -// This component is loaded dynamically -const SomeComponent = React.lazy(() => import('./SomeComponent')); +import MarkdownPreview from './MarkdownPreview.js'; ``` - +To defer loading this component's code until it's rendered for the first time, replace this import with: - +```js +import { lazy } from 'react'; + +const MarkdownPreview = lazy(() => import('./MarkdownPreview.js')); +``` + +This code relies on [dynamic `import()`,](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) which might require support from your bundler or framework. + +Now that your component's code loads on demand, you also need to specify what should be displayed while it is loading. You can do this by wrapping the lazy component or any of its parents into a [``](/apis/react/Suspense) boundary: + +```js {1,4} +}> +

Preview

+ +
+``` + +In this example, the code for `MarkdownPreview` won't be loaded until you attempt to render it. If `MarkdownPreview` hasn't loaded yet, `Loading` will be shown in its place. Try ticking the checkbox: + + + +```js App.js +import { useState, Suspense, lazy } from 'react'; +import Loading from './Loading.js'; + +const MarkdownPreview = lazy(() => delayForDemo(import('./MarkdownPreview.js'))); + +export default function MarkdownEditor() { + const [showPreview, setShowPreview] = useState(false); + const [markdown, setMarkdown] = useState('Hello, **world**!'); + return ( + <> +