useEffect vs useLayoutEffect
Question
How does useLayoutEffect differ from useEffect?
Hint
Prefer useEffect; use useLayoutEffect only for DOM measurements.
Explanation
useLayoutEffect runs synchronously after DOM mutations but before the browser paints, blocking visual updates. useEffect runs asynchronously after paint. Use useLayoutEffect when you need to read DOM layout and synchronously re-render before paint (e.g., measuring element size, positioning tooltips). For most effects, useEffect is preferred — it does not block painting. useLayoutEffect can cause performance issues if overused. In SSR, useLayoutEffect warns and does nothing; use useIsomorphicLayoutEffect pattern.
Related Resources
Related Lessons
- useEffect
Learn useEffect.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.