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:
NetworkErrorFallbackfor API failuresNotFoundFallbackfor missing dataPermissionErrorFallbackfor 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.