introduction
Many friends may not have used it in the projectSuspense
,butSuspense
yesReact
A very important part of future development.
This article will explainSuspense
forReact
The meaning of
React iterative process
React
The main features from v16 to v18 have undergone three major changes:
- v16: Async Mode (asynchronous mode)
- v17: Concurrent Mode (concurrent mode)
- v18: Concurrent Render (Concurrent Update)
To understand the significance of these three changes, you need to understand firstReact
A very confusing concept—render
(Render).
ClassComponent
ofrender
Function execution is calledrender
:
class App extends Component { render() { // ...This is the render function } }
Andrender
The process of rendering the result to the page is calledcommit
。
Async Mode
The purpose is to makerender
Become asynchronous and interruptible.
Concurrent Mode
The purpose is to makecommit
It is concurrent in the user's perception.
becauseConcurrent Mode
Includebreaking change
,sov18
ProposedConcurrent Render
, reduce the cost of developers' migration.
SoMake commit concurrent in user perceptionWhat does it mean?
The meaning of "concurrency"
Speaking ofconcurrent, I have to mentionSuspense
. Consider the following code:
const App = () => { const [count, setCount] = useState(0); useEffect(() => { setInterval(() => { setCount(count => count + 1); }, 1000); }, []); return ( <> <Suspense fallback={<div>loading...</div>}> <Sub count={count} /> </Suspense> <div>count is {count}</div> </> ); };
in:
- An update will be triggered every second, and the status will be
count
Updated tocount => count + 1
- exist
Sub
Asynchronous request will be initiated in the request, and before the request returns, the package will be returned.Sub
ofSuspense
Will renderfallback
Assuming that the request returns after three seconds, ideally, the pages before and after the request is initiated will be displayed in sequence as:
// Before the request within the Sub is initiated<div class=“sub”>I am sub, count is 0</div> <div>count is 0</div> // The request within the Sub was initiated for 1 second<div>loading...</div> <div>count is 1</div> // The request within the sub was initiated for the second second<div>loading...</div> <div>count is 2</div> // The request within the Sub was initiated for 3 seconds<div>loading...</div> <div>count is 3</div> // After the request within the Sub is successful<div class=“sub”>I am sub, request success, count is 4</div> <div>count is 4</div>
From the user's perspective, there are two tasks on the pageconcurrentimplement:
- ask
Sub
Task (Observe the first onediv
Changes) - Change
count
Task (observe the second onediv
Changes)
Suspense
BringedMultitasking concurrent execution on the pageFeeling that'sConcurrent
(Concurrent) inReact
Meaning in.
ActuallyAsync Mode
When it was supportedSuspense
. But the above code isAsync Mode
The page is as follows:
// Before the request within the Sub is initiated<div class=“sub”>I am sub, count is 0</div> <div>count is 0</div> // The request within the Sub was initiated for 1 second<div>loading...</div> <div>count is 0</div> // The request within the sub was initiated for the second second<div>loading...</div> <div>count is 0</div> // The request within the Sub was initiated for 3 seconds<div>loading...</div> <div>count is 0</div> // After the request within the Sub is successful<div class=“sub”>I am sub, request success, count is 4</div> <div>count is 4</div>
From the user's perspective,Request Sub TaskWhen executing,Change the count taskIt was frozen.
This is why it is calledAsync
(Async) insteadConcurrent
(concurrent).
The meaning of Suspense
You can see that forConcurrent
,Suspense
It is an indispensable part.
It can be considered thatSuspense
The function isDivide the parts of the page that need to be rendered concurrently。
For example, in the above example,Suspense
WillRequest Sub TaskandChange the count taskDivide and execute visually concurrently.
When it's clearSuspense
After the meaning ofReact
What you are doing next is to expandSuspense
scenes (that is, include more scenes in the category of concurrent rendering).
For example, the current ones are:
- pass
React
Providedfetch
Asynchronous request after library transformation useTransition
useDeferredvalue
Will join in the future:
Server Component
Selective Hydration
Summarize
React
The development history is:synchronousarriveasynchronous, and thenconcurrent。
When implementedconcurrentAfter that, the next development direction will be: Continuous expansion and useconcurrentscene.
Suspense
The function isDivide the parts of the page that need to be rendered concurrently。
This development path is fromReact
It was decided at the beginning of its birth, because from a structural perspective,React
Heavy dependence on the runtime, in order to optimize performance,concurrentIt is the optimal development direction under this architecture.
The above is the detailed content of the analysis of the significance and function of Suspense to React. For more information about the role of React Suspense, please pay attention to my other related articles!