Re:React: You Might Not Need an Effect
by zestyrx on Thursday March 23, 2023 @03:11PM
(#35279834)
Isn't this exactly what useLayoutEffect is for?
https://react.dev/reference/react/useLayoutEffect
[ Parent
| on HN ]
Re:React: You Might Not Need an Effect
by karaokeyoga on Thursday March 23, 2023 @05:21PM
(#35281450)
useLayoutEffect is called after the render cycle but before the “paint”. useMemo is called during the render cycle (i.e. immediately).
You can confirm this via console.log:
useMemo(() => console.log(1), []) console.log(2)
… will print 1 then 2.
If you replace useMemo with useLayoutEffect, you’ll get 2 then 1.