0
0
Reactframework~5 mins

Error boundaries concept in React

Choose your learning style9 modes available
Introduction

Error boundaries help catch errors in parts of your app so the whole app doesn't crash. They show a fallback UI instead of a broken screen.

When you want to prevent your entire app from crashing due to errors in a small part.
When you want to show a friendly message if a component fails to load.
When you want to log errors from components to fix bugs later.
When you want to isolate errors to specific parts of your UI.
Syntax
React
class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

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

  componentDidCatch(error, errorInfo) {
    // You can log the error here
  }

  render() {
    if (this.state.hasError) {
      return <h1>Something went wrong.</h1>;
    }
    return this.props.children;
  }
}

Error boundaries must be class components in React.

They catch errors in their child components during rendering, lifecycle methods, and constructors.

Examples
A simple error boundary showing a custom message when an error happens.
React
class ErrorBoundary extends React.Component {
  state = { hasError: false };

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

  render() {
    if (this.state.hasError) {
      return <div>Oops! Error caught.</div>;
    }
    return this.props.children;
  }
}
This example logs the error to the console for debugging.
React
class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

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

  componentDidCatch(error, errorInfo) {
    console.error('Error caught:', error, errorInfo);
  }

  render() {
    if (this.state.hasError) {
      return <h2>Failed to load this part.</h2>;
    }
    return this.props.children;
  }
}
Sample Program

This app uses an error boundary to catch an error thrown by BuggyComponent. Instead of crashing, it shows a friendly message.

React
import React from 'react';

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

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

  componentDidCatch(error, errorInfo) {
    console.log('Error caught:', error.message);
  }

  render() {
    if (this.state.hasError) {
      return <h1>Oops! Something went wrong.</h1>;
    }
    return this.props.children;
  }
}

function BuggyComponent() {
  throw new Error('Bug happened!');
  // return <div>This will not render.</div>;
}

export default function App() {
  return (
    <ErrorBoundary>
      <BuggyComponent />
    </ErrorBoundary>
  );
}
OutputSuccess
Important Notes

Error boundaries do not catch errors inside event handlers.

Use multiple error boundaries to isolate errors in different parts of your app.

Always provide a fallback UI to keep your app user-friendly.

Summary

Error boundaries catch errors in child components to prevent app crashes.

They must be class components with special lifecycle methods.

Use them to show fallback UI and log errors for better user experience.