codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚛️

React.js

41 / 61 topics
40Error Boundaries in React41Handling Errors in Class Components42Using the Error Boundary API
Tutorials/React.js/Handling Errors in Class Components
⚛️React.js

Handling Errors in Class Components

Updated 2026-04-20
3 min read

Introduction

In this section, we will delve into error handling within functional components in React.js using modern hooks like useState and useEffect. Error boundaries are a special type of React component 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. This is particularly useful for preventing entire apps from crashing due to unhandled exceptions.

What Are 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. They work like try-catch blocks but for the entire component tree.

Key Points About Error Boundaries

  • Catch Errors: Only class components can be error boundaries. In the future, functional components might also support this feature.
  • Fallback UI: When an error boundary catches an error, it displays a fallback UI instead of the component tree that crashed.
  • Logging Errors: They log errors to help developers understand what went wrong.

Implementing Error Boundaries

To create an error boundary, you need to define two lifecycle methods in your class component: static getDerivedStateFromError() and componentDidCatch(). These methods work together to handle errors gracefully. However, since we are using functional components, we will use hooks to achieve similar functionality.

Step 1: Define the Error Boundary Component

Here is a basic example of how to implement an error boundary in a functional component:

import React, { useState } from 'react';

const ErrorBoundary = ({ children }) => {
  const [hasError, setHasError] = useState(false);

  // Function to handle errors
  const handleError = (error) => {
    console.error("An error occurred:", error);
    setHasError(true);
  };

  if (hasError) {
    // Fallback UI when there's an error
    return <h1>Something went wrong.</h1>;
  }

  return (
    <React.Fragment>
      {children}
    </React.Fragment>
  );
};

export default ErrorBoundary;

Explanation

  • useState(): This hook is used to manage the state of whether an error has occurred. Initially, hasError is set to false.

  • handleError(error): This function logs the error and updates the state to indicate that an error has occurred.

Step 2: Use the Error Boundary

Once you have defined your error boundary, you can wrap any part of your component tree in it:

import React from 'react';
import ReactDOM from 'react-dom';
import ErrorBoundary from './ErrorBoundary';
import MyComponent from './MyComponent';

const App = () => {
  return (
    <div>
      <h1>Welcome to the App</h1>
      {/* Wrap the component that might throw an error */}
      <ErrorBoundary>
        <MyComponent />
      </ErrorBoundary>
    </div>
  );
};

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

Best Practices for Error Boundaries

  • Catch Specific Errors: If possible, catch specific errors and provide different fallback UIs based on the error type.

  • Log Errors: Use handleError() to log errors to an error reporting service like Sentry or LogRocket. This helps in debugging and monitoring your application.

  • Fallback UI: Design a user-friendly fallback UI that provides information about what went wrong and possibly how to recover from it.

  • Avoid Catching Too Broadly: Be cautious about catching too many types of errors, as this can mask underlying issues in your codebase. It's better to handle specific error scenarios.

Handling Errors in Functional Components

While class components are the only ones that can currently act as error boundaries, you can still handle errors in functional components using higher-order components (HOCs) or custom hooks.

Using Higher-Order Components (HOCs)

Here’s an example of how to use an HOC to wrap a functional component with an error boundary:

import React from 'react';
import ErrorBoundary from './ErrorBoundary';

const MyComponent = () => {
  // This component might throw an error
  return <div>{10 / 0}</div>;
};

// Higher-order component to wrap the functional component
const withErrorBoundary = (WrappedComponent) => {
  const EnhancedComponent = ({ ...props }) => (
    <ErrorBoundary>
      <WrappedComponent {...props} />
    </ErrorBoundary>
  );

  return EnhancedComponent;
};

export default withErrorBoundary(MyComponent);

Using Custom Hooks

Alternatively, you can create a custom hook to handle errors in functional components:

import React, { useState } from 'react';

const useErrorHandler = () => {
  const [hasError, setHasError] = useState(false);

  const handleError = (error) => {
    console.error("An error occurred:", error);
    setHasError(true);
  };

  return { hasError, handleError };
};

export default useErrorHandler;

Then, in your functional component:

import React from 'react';
import useErrorHandler from './useErrorHandler';

const MyComponent = () => {
  const { hasError, handleError } = useErrorHandler();

  if (hasError) {
    return <h1>Something went wrong.</h1>;
  }

  try {
    // Code that might throw an error
    return <div>{10 / 0}</div>;
  } catch (error) {
    handleError(error);
  }
};

export default MyComponent;

Conclusion

Error boundaries are a powerful feature in React.js for handling errors gracefully. By catching and logging errors, you can prevent your application from crashing and provide a better user experience with fallback UIs. Whether using class components or functional components, there are strategies to implement robust error handling in your React applications.

Remember, while error boundaries help manage runtime errors, they should not replace proper testing and code quality practices. Always strive for writing clean, maintainable code to minimize the occurrence of errors in the first place.


PreviousError Boundaries in ReactNext Using the Error Boundary API

Recommended Gear

Error Boundaries in ReactUsing the Error Boundary API