useLayoutEffect
A version of useEffect that runs synchronously after React changes the DOM but before the browser paints the screen.
In more detail
Use it to measure elements, for example with getBoundingClientRect, and adjust positions before the user sees anything, which prevents flicker. Because it blocks painting, keep it short and prefer useEffect for everything else.
Related terms
- useEffect — The React hook for running side work, such as adding listeners or changing document.body, after a component renders.
- paint — The rendering stage that records how to fill in each box's pixels: text, colours, borders, shadows and images.
- flicker — A brief visible jump where the screen shows one layout and then quickly redraws another.
- getBoundingClientRect — A browser method that returns an element size and its position relative to the visible part of the page.
Taught in
- Avoiding Effect Antipatterns: Derived State And Events — React 19 & Modern Component Architecture
- useLayoutEffect vs useEffect: Painting And Synchronous Reads — React 19 & Modern Component Architecture