1.0.10 • Published 8 months ago

rinshad-react-guard v1.0.10

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

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

Error Boundary 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

PropTypeDefaultDescription
childrenReactNoderequiredThe components to be wrapped by the error boundary
excludedKeywordsstring[][]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

// 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