Choosing Between useEffect and useLayoutEffect
You'll learn: A simple decision framework for picking the right effect hook every single time.
The Default Choice Rule
Start with useEffect. Always. It's the safe, performant default that works for 95% of situations. Only switch to useLayoutEffect when you have a specific, visible problem to solve.
The Decision Framework
Think of choosing between these hooks like choosing between regular mail and overnight delivery:
Use useEffect (regular mail) when:
- Fetching data from an API
- Setting up subscriptions
- Logging analytics
- Updating document title
- Any side effect where a tiny delay doesn't matter visually
Upgrade to useLayoutEffect (overnight delivery) when:
- You see visual flicker on screen
- You need to measure DOM elements before the browser paints
- You're making layout changes that would cause visible jumps
- Reading scroll positions or element dimensions immediately after render
The Upgrade Path
Don't guess which one you need upfront. Follow this process:
- Write your effect with
useEffect - Test in the browser
- Do you see flicker, jumps, or layout shifts?
- Only then switch to
useLayoutEffect
This approach keeps your app performant by default and only adds synchronous blocking behavior where absolutely necessary.
Common Mistake
Don't use useLayoutEffect "just to be safe" or because the side effect feels important. The performance cost is real, and most effects don't need synchronous execution.
Key Takeaway: Default to useEffect for everything, then upgrade to useLayoutEffect only when you observe visual problems that need fixing—not before.