What are error boundaries, and how do they work?

Learn how error boundaries catch runtime errors in React components and prevent full app crashes.

advancedError handlingerror boundariescomponentDidCatchreact error handling
Published: 10/26/2025
Updated: 10/26/2025

Error Boundaries are special components that catch JavaScript errors in their child components.


⚙️ Example

class ErrorBoundary extends React.Component {
  state = { hasError: false };

  static getDerivedStateFromError() {
    return { hasError: true };
  }

  componentDidCatch(error, info) {
    console.log(error, info);
  }

  render() {
    return this.state.hasError ? (
      <h2>Something went wrong.</h2>
    ) : (
      this.props.children
    );
  }
}

âś… Use them around risky components like lazy-loaded or API-heavy ones.


Stay Updated

Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.