Suspense
一句话解释
Suspense 是 React 组件那个 shows fallback UI 虽然它的 children 是加载 — used 使用 lazy 加载 and 数据获取.
用大白话说
<Suspense> lets you specify a loading indicator (fallback) for a part of the UI that isn't ready yet. When a child "suspends" — because it's lazy-loading code or fetching data — React shows the fallback instead, and swaps in the real content when it's ready.
<Suspense fallback={<Spinner />}><MyComponent /></Suspense> — if MyComponent suspends, the Spinner shows. When MyComponent is ready, it renders.
Suspense works with React.lazy for code splitting and, in React 18+, with concurrent features like use() for data fetching. You can nest Suspense boundaries to show different fallbacks for different parts of the page.
示例
<Suspense fallback={<div>Loading chart...</div>}>
<LazyChart />
</Suspense>虽然 LazyChart's code 是 downloading, '加载 chart...' fallback 是 shown. 一次加载, chart 渲染.
相关术语
相关资源
相关课程
- Lazy 加载
学习 Suspense for 异步 UI.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。