
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
react-error-boundary
Advanced tools
The react-error-boundary package provides a simple and reusable wrapper component that catches JavaScript errors in their child component tree, logs those errors, and displays a fallback UI instead of the component tree that crashed. It is designed to handle errors that occur during rendering, in lifecycle methods, and in constructors of the whole tree below them.
Error Boundary Wrapper
This feature allows you to wrap your components with an ErrorBoundary component. You can specify a FallbackComponent to be displayed when an error is caught, and an onError handler to log or report errors.
{"<ErrorBoundary FallbackComponent={MyFallbackComponent} onError={myErrorHandler}>
<MyWidget />
</ErrorBoundary>"}
useErrorHandler Hook
The useErrorHandler hook can be used within functional components to handle errors. When an error is caught, it can be passed to the handleError function provided by the hook, which will then trigger the nearest error boundary.
{"const handleError = useErrorHandler();
try {
// Some operation that might fail
} catch (error) {
handleError(error);
}"}
This package provides an overlay for displaying runtime errors in React applications. It is similar to react-error-boundary in that it helps developers handle errors, but it focuses on providing a development-time overlay that displays errors rather than a production-ready error boundary component.
React-error-guard is a package that provides error boundary functionality with a focus on simplicity and minimalism. It is similar to react-error-boundary but may have fewer features and a simpler API, which could be beneficial for smaller projects or for developers who prefer a more straightforward approach.
A simple, reusable React error boundary component for React 16+.
React v16 introduced the concept of “error boundaries”.
This component provides a simple and reusable wrapper that you can use to wrap around your components. Any rendering errors in your components hierarchy can then be gracefully handled.
The simplest way to use <ErrorBoundary>
is to wrap it around any component that may throw an error.
This will handle errors thrown by that component and its descendants too.
import ErrorBoundary from 'react-error-boundary';
<ErrorBoundary>
<ComponentThatMayError />
</ErrorBoundary>
You can react to errors (e.g. for logging) by providing an onError
callback:
import ErrorBoundary from 'react-error-boundary';
const myErrorHandler = (error: Error, componentStack: string) => {
// Do something with the error
// E.g. log to an error logging client here
};
<ErrorBoundary onError={myErrorHandler}>
<ComponentThatMayError />
</ErrorBoundary>
You can also customize the fallback component’s appearance:
import { ErrorBoundary } from 'react-error-boundary';
const MyFallbackComponent = ({ componentStack, error }) => (
<div>
<p><strong>Oops! An error occured!</strong></p>
<p>Here’s what we know…</p>
<p><strong>Error:</strong> {error.toString()}</p>
<p><strong>Stacktrace:</strong> {componentStack}</p>
</div>
);
<ErrorBoundary FallbackComponent={MyFallbackComponent}>
<ComponentThatMayError />
</ErrorBoundary>
You can also use it as a higher-order component:
import { ErrorBoundaryFallbackComponent, withErrorBoundary } from 'react-error-boundary';
const ComponentWithErrorBoundary = withErrorBoundary(
ComponentThatMayError,
ErrorBoundaryFallbackComponent, // Or pass in your own fallback component
onErrorHandler: (error, componentStack) => {
// Do something with the error
// E.g. log to an error logging client here
},
);
<ComponentWithErrorBoundary />
FAQs
Simple reusable React error boundary component
The npm package react-error-boundary receives a total of 4,116,626 weekly downloads. As such, react-error-boundary popularity was classified as popular.
We found that react-error-boundary demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.