Skip to main content
InterviewHubPRO
Section Overview
Q375

Question 375 of 407

Part 9: React Hooks Deep Dive (262 - 316)

92%
React Core & Hooks (407 Qs)•Part 9: React Hooks Deep Dive (262 - 316)

Why do you not need error boundaries for event handlers?

mid LevelCode ExampleHooks

Error boundaries do not catch errors inside event handlers.

vbnet
React doesn’t need error boundaries to recover from errors in event handlers. Unlike the render method and lifecycle methods, the event handlers don’t happen during rendering. So if they throw, React still knows what to display on the screen.

If you need to catch an error inside an event handler, use the regular JavaScript try / catch statement:
javascript
    class MyComponent extends React.Component {
      constructor(props) {
        super(props);
        this.state = { error: null };
        this.handleClick = this.handleClick.bind(this);
      }

      handleClick() {
        try {
          // Do something that could throw
        } catch (error) {
          this.setState({ error });
        }
      }

      render() {
        if (this.state.error) {
          return <h1>Caught an error.</h1>;
        }
        return <button onClick={this.handleClick}>Click Me</button>;
      }
    }
vbnet
Note that the above example is demonstrating regular JavaScript behavior and doesn’t use error boundaries.