Error boundaries help your app stay working even when some parts have problems. They catch errors in components so the whole app doesn't crash.
Why error boundaries matter in NextJS
Start learning this pattern below
Jump into concepts and practice - no test required
import { ErrorBoundary } from 'react-error-boundary'; function ErrorFallback({ error, resetErrorBoundary }) { return ( <div role="alert"> <p>Something went wrong:</p> <pre>{error.message}</pre> <button onClick={resetErrorBoundary}>Try again</button> </div> ); } function App() { return ( <ErrorBoundary FallbackComponent={ErrorFallback} onReset={() => { // reset the state of your app here }} > <YourComponent /> </ErrorBoundary> ); }
Error boundaries catch errors in their child components during rendering, lifecycle methods, and constructors.
They do not catch errors inside event handlers.
import { ErrorBoundary } from 'react-error-boundary'; function ErrorFallback({ error }) { return <div>Error: {error.message}</div>; } function App() { return ( <ErrorBoundary FallbackComponent={ErrorFallback}> <MyComponent /> </ErrorBoundary> ); }
import { ErrorBoundary } from 'react-error-boundary'; function ErrorFallback({ error, resetErrorBoundary }) { return ( <div role="alert"> <p>Something went wrong:</p> <pre>{error.message}</pre> <button onClick={resetErrorBoundary}>Try again</button> </div> ); } function App() { return ( <ErrorBoundary FallbackComponent={ErrorFallback} onReset={() => { // reset app state }} > <MyComponent /> </ErrorBoundary> ); }
This example shows a counter button that crashes when it reaches 3. The error boundary catches the crash and shows a friendly message with a retry button.
import React, { useState } from 'react'; import { ErrorBoundary } from 'react-error-boundary'; function BuggyCounter() { const [count, setCount] = useState(0); if (count === 3) { throw new Error('I crashed at 3!'); } return <button onClick={() => setCount(count + 1)}>Count: {count}</button>; } function ErrorFallback({ error, resetErrorBoundary }) { return ( <div role="alert"> <p>Oops! Something went wrong:</p> <pre>{error.message}</pre> <button onClick={resetErrorBoundary}>Try again</button> </div> ); } export default function App() { return ( <ErrorBoundary FallbackComponent={ErrorFallback} onReset={() => { // reset logic if needed }} > <BuggyCounter /> </ErrorBoundary> ); }
Use error boundaries to prevent your whole app from crashing due to one component's error.
Always provide a fallback UI so users know something went wrong.
Resetting error boundaries helps users recover without reloading the page.
Error boundaries catch errors in parts of your app to keep it running.
They improve user experience by showing friendly messages instead of broken screens.
Use them to isolate errors and allow recovery with reset options.
Practice
Solution
Step 1: Understand error boundaries role
Error boundaries catch errors in React components during rendering, lifecycle methods, and constructors.Step 2: Identify their main benefit
They prevent the whole app from crashing by showing a fallback UI instead of a broken screen.Final Answer:
To catch JavaScript errors in components and display a fallback UI -> Option AQuick Check:
Error boundaries catch errors = B [OK]
- Confusing error boundaries with authentication
- Thinking error boundaries improve SEO
- Assuming error boundaries handle server errors automatically
Solution
Step 1: Recall error boundary implementation
Error boundaries must be class components with lifecycle methods like componentDidCatch to catch errors.Step 2: Check options for correct syntax
class ErrorBoundary extends React.Component { constructor() { super(); this.state = { hasError: false }; } componentDidCatch() { this.setState({ hasError: true }); } render() { if (this.state.hasError) return <div>Error occurred</div>; return this.props.children; } } correctly defines a class component with constructor, state, componentDidCatch, and render method.Final Answer:
class ErrorBoundary extends React.Component { constructor() { super(); this.state = { hasError: false }; } componentDidCatch() { this.setState({ hasError: true }); } render() { if (this.state.hasError) return <div>Error occurred</div>; return this.props.children; } } -> Option AQuick Check:
Error boundaries require class + componentDidCatch = C [OK]
- Trying to create error boundaries as functional components
- Missing componentDidCatch lifecycle method
- Not initializing state to track errors
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
componentDidCatch(error, info) {
this.setState({ hasError: true });
}
render() {
if (this.state.hasError) {
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}
function BuggyComponent() {
throw new Error('Bug!');
return <div>No bugs</div>;
}
// Usage
<ErrorBoundary>
<BuggyComponent />
</ErrorBoundary>Solution
Step 1: Understand error throwing in child
BuggyComponent throws an error immediately when rendered.Step 2: Check error boundary response
ErrorBoundary catches the error in componentDidCatch and sets hasError to true, rendering fallback UI.Final Answer:
<h1>Something went wrong.</h1> -> Option CQuick Check:
Error caught, fallback UI shown = A [OK]
- Expecting child component output despite error
- Thinking error is uncaught and crashes app
- Assuming blank screen instead of fallback UI
function ErrorBoundary({ children }) {
try {
return children;
} catch (error) {
return <div>Error occurred</div>;
}
}Solution
Step 1: Analyze error boundary implementation
Try-catch inside a functional component does not catch errors during rendering lifecycle in React.Step 2: Recall React error boundary requirements
Error boundaries must be class components implementing componentDidCatch lifecycle method to catch errors properly.Final Answer:
Error boundaries must be class components with componentDidCatch method -> Option BQuick Check:
Functional try-catch won't catch render errors = A [OK]
- Using try-catch in functional components expecting error boundary behavior
- Assuming React has a useErrorBoundary hook
- Thinking wrapping children in fragments fixes error catching
Solution
Step 1: Understand reset behavior in error boundaries
Resetting error state allows the component tree to re-render normally after an error.Step 2: Identify correct reset method
Calling this.setState({ hasError: false }) inside the error boundary resets the error state and shows children again.Final Answer:
Add a button that calls this.setState({ hasError: false }) inside the error boundary's fallback UI -> Option DQuick Check:
Reset error state with setState = D [OK]
- Reloading page instead of resetting state
- Using try-catch around reset button unnecessarily
- Relying on automatic reset with useEffect without user action
