React provides error boundaries as a way to capture errors that occur during rendering, in lifecycle methods, or in constructors of the whole tree below them, and display a fallback UI instead of crashing the entire application. While React provides a default error boundary, you can also create a custom error boundary component to have more control over the error handling and display.
In this tutorial, we will walk through the steps to create a custom error boundary component in React.
Step 1: Set up a new React project
To get started, make sure you have Node.js and npm installed on your machine. Then, use the following command to create a new React project:
npx create-react-app error-boundary-example
Next, navigate into the project folder:
cd error-boundary-example
Step 2: Create an ErrorBoundary component
In your project folder, create a new file called ErrorBoundary.js
and add the following code:
import React, { Component } from 'react';
class ErrorBoundary extends Component {
constructor(props) {
super(props);
this.state = {
hasError: false,
error: null,
};
}
static getDerivedStateFromError(error) {
return { hasError: true, error };
}
render() {
const { hasError } = this.state;
const { fallbackComponent: FallbackComponent, children } = this.props;
if (hasError) {
return <FallbackComponent error={this.state.error} />;
}
return children;
}
}
export default ErrorBoundary;
In the ErrorBoundary
component, we define a state variable hasError
to track if an error has occurred and an error
variable to store the error object. We also implement the static method getDerivedStateFromError
to update the state when an error is caught.
In the render
method, we check if hasError
is true. If it is, we render the FallbackComponent
with the error object passed as a prop. Otherwise, we render the children
of the ErrorBoundary
component.
Step 3: Implement the Error Boundary in your application
Now let’s use the ErrorBoundary
component in our application. Open the src/App.js
file and update it as follows:
import React from 'react';
import ErrorBoundary from './ErrorBoundary';
import MyComponent from './MyComponent';
const FallbackComponent = ({ error }) => {
return (
<div>
<h1>Oops! Something went wrong!</h1>
<p>{error.toString()}</p>
</div>
);
};
function App() {
return (
<ErrorBoundary fallbackComponent={FallbackComponent}>
<div className="App">
<h1>Hello, React!</h1>
<MyComponent />
</div>
</ErrorBoundary>
);
}
export default App;
In the App
component, we import the ErrorBoundary
component and a custom MyComponent
. We also define a FallbackComponent
that will be displayed when an error occurs.
Wrap the components you want to be covered by the error boundary with the ErrorBoundary
component and pass the FallbackComponent
as a prop.
Step 4: Test the Error Boundary
Start the development server by running:
npm start
Now, if you introduce an error in the MyComponent
component or any of its child components, the ErrorBoundary
component will catch the error and display the fallback UI, rendering the FallbackComponent
we defined in the App
component.
Conclusion
In this tutorial, you have learned how to create a custom error boundary component in React. Error boundaries are a powerful tool for handling errors without crashing the entire application. With a custom error boundary component, you can create a more user-friendly error handling experience by providing a fallback UI. Utilize error boundaries in your React applications to handle errors gracefully and enhance user experience.
#react #errorboundary