rinshad-react-guard
v1.0.10
Published
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
Downloads
374
Maintainers
Readme
rinshad-react-guard
A robust React error boundary solution developed by 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-friendly error page while storing error details in session storage.
Features
- 🛡️ Prevents white screen crashes in React applications
- 🎯 Custom error page with clean UI
- 💾 Stores error messages in session storage
- 🔍 Configurable error filtering
- ⚡ Compatible with Vite and React (jsx/tsx)
- 🔄 Handles both sync and async errors
- 🎨 Customizable styling
Installation
npm install rinshad-react-guard
or
yarn add rinshad-react-guard
Demo
Try it live: Demo Project
Usage
Basic Setup
Wrap your app's root component with the ErrorBoundary component:
import ErrorBoundary from 'rinshad-react-guard';
function App() {
return (
<ErrorBoundary>
<YourApp />
</ErrorBoundary>
);
}
Advanced Usage with Excluded Keywords
You can exclude certain errors from being caught by providing keywords:
import ErrorBoundary from 'rinshad-react-guard';
function App() {
const excludedKeywords = ['ChunkLoadError', 'NetworkError'];
return (
<ErrorBoundary excludedKeywords={excludedKeywords}>
<YourApp />
</ErrorBoundary>
);
}
Accessing Error Messages
Error messages are automatically stored in session storage with the key rinshadReactGuardAlert
. You can access them programmatically:
const errorMessage = sessionStorage.getItem('rinshadReactGuardAlert');
API Reference
ErrorBoundary Props
| 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
- The ErrorBoundary component wraps your application and listens for any JavaScript errors
- 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
- Users can return to the home page using the provided button
- Browser back button navigation is handled to prevent users from seeing the error state again
Best Practices
- Place the ErrorBoundary at the root level of your application
- Use multiple ErrorBoundary components for different sections if needed
- Configure excluded keywords based on your application's needs
- Implement proper error logging and monitoring
Example
// 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 (
<ErrorBoundary excludedKeywords={excludedKeywords}>
<YourApp />
</ErrorBoundary>
);
}
export default App;
Browser Support
- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Edge (latest)
License
MIT License
Support
For support, email [email protected]
Made with ❤️ by rinshad.com