📖 Scenario: You are building a React app that shows user profiles. Sometimes, the profile data might cause errors when rendering. To keep the app running smoothly, you want to catch these errors and show a friendly message instead of a broken screen.
🎯 Goal: Create a React error boundary component that catches errors in its child components and displays a fallback UI. Then use this error boundary to wrap a component that might throw an error.
📋 What You'll Learn
Create a class component called ErrorBoundary that implements componentDidCatch and getDerivedStateFromError methods.
Add a state property hasError to track if an error happened.
Render a fallback UI with the text Something went wrong. when an error is caught.
Wrap a child component called UserProfile inside ErrorBoundary in the main app component.
💡 Why This Matters
🌍 Real World
Error boundaries help keep React apps running smoothly by catching unexpected errors in parts of the UI and showing friendly messages instead of crashing the whole app.
💼 Career
Understanding error boundaries is important for React developers to build resilient user interfaces and improve user experience by handling errors gracefully.
Progress0 / 4 steps
1
Create the UserProfile component
Create a functional React component called UserProfile that returns a <div> with the text User Profile Loaded.
React
Hint
Use a simple function that returns JSX with the exact text inside a div.
2
Create the ErrorBoundary class component
Create a class component called ErrorBoundary that extends React.Component. Add a state property hasError initialized to false.
React
Hint
Use a constructor to set initial state with hasError: false.
3
Add error handling methods to ErrorBoundary
Inside ErrorBoundary, add a static method getDerivedStateFromError that sets hasError to true. Also add componentDidCatch that receives error and info parameters but does nothing inside.
React
Hint
Use getDerivedStateFromError to update state and show fallback UI in render.
4
Use ErrorBoundary to wrap UserProfile in the main app
Create a functional component called App that returns ErrorBoundary wrapping UserProfile. Export App as default.
React
Hint
Wrap UserProfile inside ErrorBoundary tags in the App component.
Practice
(1/5)
1. What is the main purpose of an error boundary in React?
easy
A. To handle user input validation in forms
B. To manage state updates in functional components
C. To catch JavaScript errors in child components and display a fallback UI
D. To optimize rendering performance by memoizing components
Solution
Step 1: Understand error boundaries role
Error boundaries catch errors in the rendering phase of child components to prevent the whole app from crashing.
Step 2: Identify the correct purpose
They show a fallback UI instead of letting the error crash the app, improving user experience.
Final Answer:
To catch JavaScript errors in child components and display a fallback UI -> Option C
Quick Check:
Error boundaries catch errors = D [OK]
Hint: Error boundaries catch errors and show fallback UI [OK]
Common Mistakes:
Thinking error boundaries manage state
Confusing error boundaries with performance tools
Assuming error boundaries handle form validation
2. Which of the following is the correct way to define an error boundary component in React?
easy
A. A class component implementing static getDerivedStateFromError and componentDidCatch methods
B. A functional component using useErrorBoundary hook
C. A class component with only render method
D. A functional component with useEffect to catch errors
Solution
Step 1: Recall error boundary requirements
Error boundaries must be class components that implement static getDerivedStateFromError and componentDidCatch lifecycle methods.
Step 2: Match the correct syntax
Only a class component implementing static getDerivedStateFromError and componentDidCatch methods correctly describes this pattern; hooks or functional components cannot be error boundaries.
Final Answer:
A class component implementing static getDerivedStateFromError and componentDidCatch methods -> Option A
Quick Check:
Error boundaries require class + lifecycle methods = B [OK]
Hint: Error boundaries must be class components with special lifecycle methods [OK]
Common Mistakes:
Trying to use hooks for error boundaries
Omitting getDerivedStateFromError method
Using only render method without error handling
3. Given this error boundary component, what will be rendered if BuggyComponent throws an error?