ReactJS – Error Boundaries

Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed. Error boundaries are particularly useful for handling errors in a way that does not break the entire application.

Important Points:

  • Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.
  • They do not catch errors inside event handlers.
  • Error boundaries should be class components because they need to use lifecycle methods.

Example

Here’s a basic example demonstrating how to create and use an error boundary in a React application.

import React from 'react';
import ReactDOM from 'react-dom';

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

  static getDerivedStateFromError(error) {
    // Update state so the next render shows the fallback UI.
    return { hasError: true };
  }

  componentDidCatch(error, errorInfo) {
    // You can also log the error to an error reporting service
    console.log('Error:', error);
    console.log('Error Info:', errorInfo);
  }

  render() {
    if (this.state.hasError) {
      // Fallback UI when an error is caught
      return <h1>Something went wrong.</h1>;
    }

    return this.props.children; 
  }
}

// BuggyComponent for demonstration
class BuggyComponent extends React.Component {
  render() {
    // Simulate a JS error
    if (this.props.triggerError) {
      throw new Error('I crashed!');
    }
    return <div>No error here!</div>;
  }
}

// App Component
class App extends React.Component {
  render() {
    return (
      <div>
        <h1>React Error Boundaries Example</h1>
        <ErrorBoundary>
          <BuggyComponent triggerError={false} />
        </ErrorBoundary>
        <ErrorBoundary>
          <BuggyComponent triggerError={true} />
        </ErrorBoundary>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));

Explanation of the Example

  1. ErrorBoundary Component:
    • constructor(props): Initializes the state with hasError: false.
    • static getDerivedStateFromError(error): Updates the state to indicate an error has occurred.
    • componentDidCatch(error, errorInfo): Logs the error and error information. This is where you can integrate error reporting services.
    • render(): If hasError is true, it renders a fallback UI (<h1>Something went wrong.</h1>). Otherwise, it renders its children.
  2. BuggyComponent:
    • A simple component that throws an error when triggerError prop is true. Otherwise, it renders a normal message.
  3. App Component:
    • Demonstrates the usage of the ErrorBoundary by wrapping BuggyComponent instances. The first instance has triggerError set to false and renders normally. The second instance has triggerError set to true, causing it to throw an error, which is caught by the ErrorBoundary.

By using this pattern, you can ensure that your application remains functional even when parts of it encounter errors, providing a better user experience and easier debugging.


Tutorials Deck

TutorialsDeck is striving to provide the best learning material on technical and non-technical subjects.

Languages

Web Technologies

Database

Trending Technologies

© 2024. All rights reserved.

Contact Us @ tutorialsdeck06@gmail.com