Course contentsShow
← React
Lesson 1315 of 2,39633. Error BoundariesFree lesson

Generic Error Fallback Components

Build reusable error fallback components that display friendly messages and retry buttons for common error scenarios.

Generic Error Fallback Components

You'll learn how to build reusable error fallback UI components that gracefully handle errors across your application.

The Core Idea

Instead of writing custom error displays for every error boundary, you create generic, reusable fallback components that can work anywhere. Think of it like having a universal "something went wrong" template that looks professional and provides helpful actions, rather than custom-building a new error page every time.

These components typically accept the error details as props and render a friendly message, the error information (in development), and action buttons like "Try Again" or "Go Home."

Basic Reusable Fallback

interface ErrorFallbackProps {
  error: Error;
  resetErrorBoundary: () => void;
}

function GenericErrorFallback({ error, resetErrorBoundary }: ErrorFallbackProps) {
  return (
    <div style={{ padding: '2rem', textAlign: 'center' }}>
      <h2>Something went wrong</h2>
      <p style={{ color: '#666' }}>
        We're sorry for the inconvenience. Please try again.
      </p>
      {process.env.NODE_ENV === 'development' && (
        <pre style={{ textAlign: 'left', background: '#f5f5f5', padding: '1rem' }}>
          {error.message}
        </pre>
      )}
      <button onClick={resetErrorBoundary}>
        Try Again
      </button>
    </div>
  );
}

Usage with Error Boundary

<ErrorBoundary fallback={(props) => <GenericErrorFallback {...props} />}>
  <RiskyComponent />
</ErrorBoundary>

Common Patterns

Create specialized variants for different scenarios:

  • NetworkErrorFallback for API failures
  • NotFoundFallback for missing data
  • PermissionErrorFallback for auth issues

Pass the appropriate fallback component to error boundaries based on context, while keeping the core structure consistent.

Key Takeaway: Generic error fallback components centralize your error UI logic, making your app's error handling consistent, maintainable, and user-friendly across all error boundaries.