Hook Errors During Render
You'll learn how error boundaries intercept errors thrown from React hooks during the render phase.
Core Concept
When you use hooks like useState, useEffect, or useMemo, certain parts of their execution happen during render. If these parts throw an error, React's error boundary system catches them just like any other render error.
Think of it like this: Error boundaries are safety nets positioned around your component tree. Hooks are just specialized functions that run while React is building your UI. If a hook explodes during that construction phase, the safety net catches it.
What Gets Caught
Synchronous hook errors during render:
useStateinitialization function throwinguseMemocomputation function throwinguseEffectsetup throwing (before the effect runs)- Custom hooks throwing during render logic
- Invalid hook calls (though these are usually React warnings)
When useState(() => { throw new Error('boom') }) executes, or when your useMemo calculation fails, that error happens during render. The error boundary around your component will catch it and display its fallback UI instead of crashing the entire app.
The Key Distinction
Remember: the synchronous part matters. If useEffect's setup function throws immediately when React calls it during the commit phase, that's caught. But if your effect returns a cleanup function that throws later, or if async code inside the effect throws, those escape error boundaries (as you learned previously).
Key Takeaway: Errors thrown synchronously inside hooks during the render phase (like useState initialization or useMemo computation) are caught by error boundaries, giving you graceful degradation when hook logic fails.