I. The execution process of useEffect and useLayoutEffect
The first thing to note is that useLayoutEffect and useEffect are very similar and have the same function signature. The only difference is that useEffect is executed asynchronously, while useLayoutEffect is executed synchronously. When the function component is refreshed (rendered), the whole process of the component containing the useEffect runs as follows.
- component re-rendering is triggered (by changing the component state or the component’s parent component re-rendering, causing the child nodes to render).
- the component function is executed.
- the component is rendered and rendered to the screen.
- the
useEffect
hook is executed.
When the function component is refreshed (rendered), the entire operation of the component containing the useLayoutEffect is as follows.
- component re-rendering is triggered (by changing the component state or the component’s parent component re-rendering, causing the child component to render).
- the component function is executed.
useLayoutEffect
hook is executed, and React waits for the useLayoutEffect function to finish executing.- The component is rendered and rendered to the screen.
The advantage of usingEffect asynchronously is that react renders the component without having to wait for the useEffect function to finish executing, causing blocking.
99% of the cases, using useEffect is fine. The only case where we need to use useLayoutEffect is that in the case of useEffect, our screen will flicker (the component is rendered twice in a short period of time).
For example, in the code below, the component renders twice.
|
|
In the above code, if you use useEffect, when you click on the div, there will be a short flicker and the screen will appear first with 0 and then with the number set in useEffect, but with useLayoutEffect there will be no flicker.
II. Using useRef to get the previous state of the component
After understanding the execution process of useEffect, we can use useRef to get the state and props of the previous state of the component, please see the following example.
|
|
Since useEffect will be executed after the component function is executed, preCountRef will store the previous value.