A robust React error boundary solution developed by [rinshad.com](https://rinshad.com) to elegantly handle runtime errors in React applications. Instead of showing the dreaded white screen of death, this library captures errors and displays a user-friendl
npm install rinshad-react-guardbash
npm install rinshad-react-guard
`
or
`bash
yarn add rinshad-react-guard
`
Demo
!Error Boundary Demo
Try it live: Demo Project
Usage
$3
Wrap your app's root component with the ErrorBoundary component:
`jsx
import ErrorBoundary from 'rinshad-react-guard';
function App() {
return (
);
}
`
$3
You can exclude certain errors from being caught by providing keywords:
`jsx
import ErrorBoundary from 'rinshad-react-guard';
function App() {
const excludedKeywords = ['ChunkLoadError', 'NetworkError'];
return (
);
}
`
$3
Error messages are automatically stored in session storage with the key rinshadReactGuardAlert. You can access them programmatically:
`javascript
const errorMessage = sessionStorage.getItem('rinshadReactGuardAlert');
`
API Reference
$3
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| children | ReactNode | required | The components to be wrapped by the error boundary |
| excludedKeywords | string[] | [] | Array of keywords to exclude from error catching |
How It Works
1. The ErrorBoundary component wraps your application and listens for any JavaScript errors
2. When an error occurs:
- The error message is captured and stored in session storage
- The user is shown a friendly error page instead of a white screen
- The error details are logged to the console for debugging
3. Users can return to the home page using the provided button
4. Browser back button navigation is handled to prevent users from seeing the error state again
Best Practices
1. Place the ErrorBoundary at the root level of your application
2. Use multiple ErrorBoundary components for different sections if needed
3. Configure excluded keywords based on your application's needs
4. Implement proper error logging and monitoring
Example
`jsx
// main.jsx or App.jsx
import React from 'react';
import ErrorBoundary from 'rinshad-react-guard';
import YourApp from './YourApp';
function App() {
const excludedKeywords = ['ChunkLoadError'];
return (
);
}
export default App;
``