What if your app never crashes on users, no matter what goes wrong?
Why Handling runtime errors in React? - Purpose & Use Cases
Imagine your React app crashes completely when a small mistake happens, like a missing value or a broken API call, leaving users staring at a blank screen.
Manually checking every possible error everywhere is exhausting and easy to forget. Without proper error handling, your app becomes unreliable and frustrating for users.
React's error boundaries catch runtime errors in components and let you show a friendly message instead of a crash, keeping your app stable and user-friendly.
componentDidCatch(error) { console.log(error); } // but UI still breaksclass ErrorBoundary extends React.Component { state = { hasError: false }; static getDerivedStateFromError() { return { hasError: true }; } componentDidCatch(error, info) { console.log(error, info); } render() { return this.state.hasError ? <h1>Something went wrong.</h1> : this.props.children; } } // Usage: <ErrorBoundary>{children}</ErrorBoundary>
You can build resilient apps that gracefully handle mistakes and keep users happy even when things go wrong.
When a user uploads a bad file or a network request fails, your app shows a helpful message instead of crashing, guiding users to fix the problem.
Runtime errors can crash your app unexpectedly.
Manual error checks are hard and incomplete.
React error boundaries catch errors and show fallback UI.
Practice
Solution
Step 1: Understand error boundaries role
Error boundaries catch errors during rendering, lifecycle methods, and constructors of child components.Step 2: Identify their main use
They prevent the entire app from crashing by showing a fallback UI instead of the broken component.Final Answer:
To catch runtime errors in child components and display a fallback UI -> Option AQuick Check:
Error boundaries catch errors = D [OK]
- Thinking error boundaries improve performance
- Confusing error boundaries with input validation
- Assuming error boundaries manage state
Solution
Step 1: Recall error boundary requirements
Error boundaries must be class components that implement static getDerivedStateFromError and componentDidCatch methods.Step 2: Check options for correct lifecycle methods
Only A class component implementing componentDidCatch and getDerivedStateFromError correctly names these methods; componentWillCatch does not exist and hooks for error boundaries are not standard.Final Answer:
A class component implementing componentDidCatch and getDerivedStateFromError -> Option AQuick Check:
Error boundaries require class + lifecycle methods = A [OK]
- Using functional components for error boundaries
- Using non-existent lifecycle methods
- Trying to catch errors inside render with try/catch
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
return { hasError: true };
}
componentDidCatch(error, info) {
console.log('Error caught:', error);
}
render() {
if (this.state.hasError) {
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}Solution
Step 1: Understand error boundary state update
When an error occurs, getDerivedStateFromError sets hasError to true, triggering fallback UI.Step 2: Check render output on error
If hasError is true, render returns <h1>Something went wrong.</h1> instead of children.Final Answer:
An error message 'Something went wrong.' will be displayed -> Option BQuick Check:
Error caught triggers fallback UI = C [OK]
- Expecting child to render despite error
- Thinking app crashes without error boundary
- Assuming infinite loop occurs
class MyErrorBoundary extends React.Component {
state = { error: null };
componentDidCatch(error, info) {
this.setState({ error });
}
render() {
if (this.state.error) {
return <div>Error occurred</div>;
}
return this.props.children;
}
}Solution
Step 1: Check error boundary lifecycle methods
To update state on error, static getDerivedStateFromError must be implemented to update state before render.Step 2: Analyze componentDidCatch usage
componentDidCatch is for side effects like logging, not for state updates; setState here can cause issues.Final Answer:
Missing static getDerivedStateFromError method -> Option CQuick Check:
State update on error requires getDerivedStateFromError = A [OK]
- Updating state only in componentDidCatch
- Thinking class fields can't hold state
- Believing error boundaries can be functional components
Solution
Step 1: Understand error boundary scope
An error boundary catches errors in its child components and their descendants.Step 2: Choose approach for single fallback UI
Wrapping the entire app in one error boundary ensures all errors show one fallback UI at top level.Final Answer:
Wrap the entire app in a single error boundary component -> Option DQuick Check:
One top-level error boundary = B [OK]
- Adding many error boundaries causing multiple fallback UIs
- Trying to catch errors with try/catch in render
- Using non-existent hooks for error boundaries
Ready to prove it?
Pass the test and this pattern moves unknown
