An error boundary is a React class component that replaces a crashed descendant tree with fallback UI. Implement a reusable ErrorBoundary that captures render and lifecycle errors, reports them, and lets the application recover manually or when a reset key changes. React documents the underlying lifecycle contract in its Component reference.
type ErrorDetails = {
error: Error;
reset: () => void;
};
type ErrorBoundaryProps = {
children: React.ReactNode;
fallback?: React.ReactNode | ((details: ErrorDetails) => React.ReactNode);
onError?: (error: Error, info: React.ErrorInfo) => void;
resetKeys?: unknown[];
};
class ErrorBoundary extends React.Component<ErrorBoundaryProps> {}
<ErrorBoundary fallback={<p>Profile unavailable</p>}>
<Profile />
</ErrorBoundary>
// If Profile throws while rendering, the paragraph replaces Profile.
<ErrorBoundary
resetKeys={[userId]}
onError={(error, info) => report(error, info)}
fallback={({ error, reset }) => (
<button onClick={reset}>Retry after: {error.message}</button>
)}
>
<UserProfile id={userId} />
</ErrorBoundary>
// The button resets manually. A changed userId also retries the children.
error and a stable reset function. Render null when fallback is omitted.onError(error, info) from componentDidCatch. Do not report manual or reset-key recovery as a new error.Object.is. A new array containing the same values must not reset.The full solution is part of Premium
Walkthrough, edge cases, complexity notes, and the runnable editor unlock with a Premium subscription.
Submissions are part of Premium
Unlock community code, comments, reactions, and framework-specific approaches.
An error boundary is a React class component that replaces a crashed descendant tree with fallback UI. Implement a reusable ErrorBoundary that captures render and lifecycle errors, reports them, and lets the application recover manually or when a reset key changes. React documents the underlying lifecycle contract in its Component reference.
type ErrorDetails = {
error: Error;
reset: () => void;
};
type ErrorBoundaryProps = {
children: React.ReactNode;
fallback?: React.ReactNode | ((details: ErrorDetails) => React.ReactNode);
onError?: (error: Error, info: React.ErrorInfo) => void;
resetKeys?: unknown[];
};
class ErrorBoundary extends React.Component<ErrorBoundaryProps> {}
<ErrorBoundary fallback={<p>Profile unavailable</p>}>
<Profile />
</ErrorBoundary>
// If Profile throws while rendering, the paragraph replaces Profile.
<ErrorBoundary
resetKeys={[userId]}
onError={(error, info) => report(error, info)}
fallback={({ error, reset }) => (
<button onClick={reset}>Retry after: {error.message}</button>
)}
>
<UserProfile id={userId} />
</ErrorBoundary>
// The button resets manually. A changed userId also retries the children.
error and a stable reset function. Render null when fallback is omitted.onError(error, info) from componentDidCatch. Do not report manual or reset-key recovery as a new error.Object.is. A new array containing the same values must not reset.The full solution is part of Premium
Walkthrough, edge cases, complexity notes, and the runnable editor unlock with a Premium subscription.
Submissions are part of Premium
Unlock community code, comments, reactions, and framework-specific approaches.
Unlock the solution & editor
Runnable editor + tests
Solve in the browser with instant Jest feedback.
Detailed solutions
Walkthroughs, edge cases, and complexity notes.
Multi-framework variants
React, Vue, Vanilla, Angular — same question, different stacks.