Skip to content

Commit e53776a

Browse files
committed
Update suspense feature
1 parent b8b12ee commit e53776a

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

README.md

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4859,25 +4859,46 @@ class ParentComponent extends React.Component {
48594859
48604860
199. ### What is suspense component?
48614861
4862-
If the module containing the dynamic import is not yet loaded by the time parent component renders, you must show some fallback content while you’re waiting for it to load using a loading indicator. This can be done using **Suspense** component.
4862+
React Suspense is a built-in feature that lets you defer rendering part of your component tree until some condition(asynchronous operation) is met—usually, data or code has finished loading. While waiting, Suspense lets you display a fallback UI like a spinner or placeholder.
48634863
4864-
For example, the below code uses suspense component,
48654864
4866-
```javascript
4867-
const OtherComponent = React.lazy(() => import("./OtherComponent"));
4865+
1. Lazy loading components uses suspense feature,
48684866
4869-
function MyComponent() {
4870-
return (
4871-
<div>
4872-
<Suspense fallback={<div>Loading...</div>}>
4873-
<OtherComponent />
4874-
</Suspense>
4875-
</div>
4876-
);
4877-
}
4878-
```
48794867
4880-
As mentioned in the above code, Suspense is wrapped above the lazy component.
4868+
If the module containing the dynamic import is not yet loaded by the time parent component renders, you must show some fallback content while you’re waiting for it to load using a loading indicator. This can be done using **Suspense** component.
4869+
4870+
```javascript
4871+
const OtherComponent = React.lazy(() => import("./OtherComponent"));
4872+
4873+
function MyComponent() {
4874+
return (
4875+
<div>
4876+
<Suspense fallback={<div>Loading...</div>}>
4877+
<OtherComponent />
4878+
</Suspense>
4879+
</div>
4880+
);
4881+
}
4882+
```
4883+
The above component shows fallback UI instead real component until `OtherComponent` is fully loaded.
4884+
4885+
2. As an another example, suspend until async data(data fetching) is ready
4886+
```jsx
4887+
function UserProfile() {
4888+
const user = use(fetchUser()); // throws a promise internally
4889+
return <div>{user.name}</div>;
4890+
}
4891+
4892+
function App() {
4893+
return (
4894+
<Suspense fallback={<div>Loading user...</div>}>
4895+
<UserProfile />
4896+
</Suspense>
4897+
);
4898+
}
4899+
4900+
```
4901+
48814902
48824903
**[⬆ Back to Top](#table-of-contents)**
48834904

0 commit comments

Comments
 (0)