Code-splitting components with lazy()
import {lazy} from 'react'
const Chart = lazy(() => import('./Chart'))
// in the render part, use `Chart` as normal
render(){
return (
<div>
// ...
<TabContent visible={visibleTab===2}>
<Chart />
</TabContent>
</div>
)
}
Avoiding spinners with Suspense
import Spinner from './Spinner'
import {Suspense} from 'react'
return (
<Suspense fallback = { <Spinner /> }>
.....
</Suspense>
)
Suspense and lazy provide an easy way for concurrent rendering.
// use hook to implement concurrent rendering
import {scheduleCallback as defer} from 'scheduler'
// defer: ...
Data fetching with React Cache
import {createResource} from 'react-cache'
const apiResource = createResource(path => {
return fetchAPI(path)
})
render(){
const data = APIResource.read(`/reviews/${this.props.id}`)
return (
...
)
}
No need to manually track whether data is empty or not. Just use Suspense.
Prerendering offscreen content
Time-slicing:
- rendering is non-blocking
- coordinate multiple updates at different priorities
- prerender content in the background with slowing down visible content
Suspense:
- Access async data from a server as easily as sync data from memory.
- Pause a component’s render without blocking siblings
- Precisely control loading states to reduce jank
lazy,Suspenseforreact16.6
Concurrent Modeforreact16.7
React Cache, Schedulerwill come soon
本文探讨了React中使用lazy()进行代码拆分,实现组件的懒加载,以及Suspense组件如何避免加载时的阻塞,提供流畅用户体验。介绍了如何使用React Cache进行数据预取,实现非阻塞渲染,并在后台预渲染内容,不减慢可见内容的显示速度。
1195

被折叠的 条评论
为什么被折叠?



