Why Event Handler Errors Aren't Caught
You'll learn: Why error boundaries won't catch errors thrown inside onClick, onChange, and other event handlers—and what to do instead.
The Render vs Event Timing Problem
Error boundaries only catch errors that happen during the React render cycle—when React is actively building or updating the component tree. Event handlers execute after rendering completes, in response to user actions like clicks or typing. By that point, React has finished its work and handed control to the browser's event system.
Think of it like a factory assembly line: error boundaries are quality control inspectors stationed on the line itself. If something breaks while building a product (rendering), they catch it. But once products leave the factory and customers use them (event handlers execute), those inspectors aren't watching anymore.
What This Means in Practice
// ❌ Error boundary won't catch this
function Button() {
const handleClick = () => {
throw new Error("Button broke!"); // Happens AFTER render
};
return <button onClick={handleClick}>Click me</button>;
}
The error above happens when the user clicks—long after React rendered the button. React's error boundary mechanism isn't monitoring that moment.
The Solution: Manual Error Handling
You need traditional try-catch blocks or error reporting:
const handleClick = () => {
try {
riskyOperation();
} catch (error) {
console.error("Event handler error:", error);
// Show user feedback, send to error tracking service, etc.
}
};
Key Takeaway: Error boundaries only protect the render phase—event handlers run outside React's control, so wrap their logic in try-catch or use error monitoring services to handle failures gracefully.