UNPKG

rinshad-react-guard

Version:

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

122 lines (98 loc) 3.57 kB
# rinshad-react-guard 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-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 ```bash npm install rinshad-react-guard ``` or ```bash yarn add rinshad-react-guard ``` ## Demo ![Error Boundary Demo](https://i.postimg.cc/SRzJ6yjX/Screenshot-2024-10-26-203648.png) Try it live: [Demo Project](https://rinshad-react-guard.netlify.app) ## Usage ### Basic Setup Wrap your app's root component with the ErrorBoundary component: ```jsx 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: ```jsx 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: ```javascript 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 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 ( <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 rinshadcm34@gmail.com --- Made with ❤️ by [rinshad.com](https://rinshad.com)