Skip to content

Commit ec0cf5c

Browse files
jtbandesalexkrolick
authored andcommitted
Update CodeSplitting to clarify that lazy() must be used with Suspense (#2306)
* Update CodeSplitting to clarify that lazy() must be used with Suspense * Update code-splitting.md
1 parent 8977759 commit ec0cf5c

File tree

1 file changed

+2
-20
lines changed

1 file changed

+2
-20
lines changed

content/docs/code-splitting.md

+2-20
Original file line numberDiff line numberDiff line change
@@ -124,37 +124,19 @@ The `React.lazy` function lets you render a dynamic import as a regular componen
124124

125125
```js
126126
import OtherComponent from './OtherComponent';
127-
128-
function MyComponent() {
129-
return (
130-
<div>
131-
<OtherComponent />
132-
</div>
133-
);
134-
}
135127
```
136128

137129
**After:**
138130

139131
```js
140132
const OtherComponent = React.lazy(() => import('./OtherComponent'));
141-
142-
function MyComponent() {
143-
return (
144-
<div>
145-
<OtherComponent />
146-
</div>
147-
);
148-
}
149133
```
150134

151-
This will automatically load the bundle containing the `OtherComponent` when this component gets rendered.
135+
This will automatically load the bundle containing the `OtherComponent` when this component is first rendered.
152136

153137
`React.lazy` takes a function that must call a dynamic `import()`. This must return a `Promise` which resolves to a module with a `default` export containing a React component.
154138

155-
### Suspense {#suspense}
156-
157-
If the module containing the `OtherComponent` is not yet loaded by the time `MyComponent` renders, we must show some fallback content while we're waiting for it to load - such as a loading indicator. This is done using the `Suspense` component.
139+
The lazy component should then be rendered inside a `Suspense` component, which allows us to show some fallback content (such as a loading indicator) while we're waiting for the lazy component to load.
158140

159141
```js
160142
const OtherComponent = React.lazy(() => import('./OtherComponent'));

0 commit comments

Comments
 (0)