ReactJS Error Boundaries
Syntax:
class MyErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
componentDidCatch(error, errorInfo) {
// You can also log the error to an error reporting service
logErrorToMyService(error, errorInfo);
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}
Example:
import MyErrorBoundary from './MyErrorBoundary';
<MyErrorBoundary>
<App />
</MyErrorBoundary>
Output:
If an error occurs inside MyErrorBoundary
, it is caught and the component returns the fallback UI instead of the children elements.
Explanation:
ReactJS Error Boundaries are components that catch JavaScript errors anywhere in their child component tree, log those errors, and display an error message instead of throwing an uncaught error to the screen. They are React components that catch errors anywhere in their child component tree, log the errors, and display a fallback UI instead of rendering the error to the screen.
Error boundaries catch errors during rendering, in lifecycle methods (such as componentDidMount
), and in constructors of these components. However, they do not catch errors that occur inside event handlers (see Handling Errors in Event Handlers for how to handle these).
Use:
ReactJS Error Boundaries allow developers to catch exceptions and errors inside the React component tree, preventing them from propagating upwards and halting a component tree’s rendering.
Use Error Boundaries to handle unexpected error cases and prevent application crashing. They allow for better error handling and improve app stability by providing a method to gracefully handle unexpected errors.
import React from 'react';
class MyComponent extends React.Component {
// ...
render() {
if (this.state.errorOccurred) {
// Render an error message
return <div>Something went wrong.</div>;
}
// Render your normal UI
return <div>{this.props.children}</div>;
}
}
Important Points:
- Error boundaries only catch errors in the components below them in the tree.
- Error boundaries catch errors during rendering, lifecycle methods, and constructors of the whole tree below them.
- Error boundaries do not catch errors in event handlers.
- Error boundaries cannot catch errors in React Hooks.
Summary:
Error boundaries in React catch and handle exceptions thrown by the child component tree. They are useful for catching errors in an entire tree of components, and prevent application crashing due to unexpected errors.
Error boundaries should be used sparingly, and the fallback solution should inform the user about the error and not directly affect the data or functionality of the application. It is important to note that Error boundaries only catch errors in components below them, and do not catch errors in event handlers or React Hooks.