I understand that one of the distinctions between useEffect
and useLayoutEffect
is as follows:
- useEffect triggered after paint
- useLayoutEffect triggered before paint and block the browser paint process
However, why does the following useEffect trigger before paint?
- useEffect triggered before paint
performance.mark('start')
const App = () => {
useEffect(() => {
performance.mark('end-useEffect')
performance.measure('useEffect triggered', 'start', 'end-useEffect');
}, [])
return (
<div>123</div>
)
}
- use useEffect and useLayoutEffect together: useEffect triggered after paint
performance.mark('start')
const App = () => {
useEffect(() => {
performance.mark('end-useEffect')
performance.measure('useEffect triggered', 'start', 'end-useEffect');
}, [])
useLayoutEffect(() => {
performance.mark('end-useLayoutEffect')
performance.measure('useLayoutEffect triggered', 'start', 'end-useLayoutEffect');
}, [])
return (
<div>123</div>
)
}
2
Answers
I found an insightful article in explaining this with exceptional detail.
https://jser.dev/2023-08-09-effects-run-paint/
Short answer:
I found a helpful article that dives into this topic and provides insights, you can refer this- https://thoughtspile.github.io/2021/11/15/unintentional-layout-effect/