📖 Scenario: You are building a React app that shows user profiles. Sometimes, a profile component might crash due to unexpected data. 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 app with an error boundary component that catches errors from a child component and displays a fallback UI.
📋 What You'll Learn
Create a functional component called UserProfile that throws an error when the user prop is null.
Create an error boundary component called ErrorBoundary using a class component with componentDidCatch and getDerivedStateFromError.
Use the ErrorBoundary component to wrap UserProfile in the main App component.
Display a fallback UI message inside ErrorBoundary when an error occurs.
💡 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 broken screens.
💼 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 accepts a user prop. If user is null, throw a new Error with the message 'User data is missing'. Otherwise, return a div that displays User: {user.name}.
React
Hint
Use a simple if check inside the function to throw an error when user is null.
2
Create the ErrorBoundary class component
Create a class component called ErrorBoundary that extends React.Component. Add a state with hasError set to false. Implement the static method getDerivedStateFromError(error) to set hasError to true. Also implement componentDidCatch(error, info) to log the error to the console. In the render method, if hasError is true, return a div with the text 'Something went wrong.'. Otherwise, return this.props.children.
React
Hint
Remember to set initial state in the constructor and update state in getDerivedStateFromError. Use componentDidCatch to log errors.
3
Use ErrorBoundary to wrap UserProfile in App
Create a functional component called App. Inside it, create a constant user set to null. Return JSX where ErrorBoundary wraps UserProfile with the user prop passed in.
React
Hint
Wrap UserProfile inside ErrorBoundary in the returned JSX of App.
4
Export the App component as default
Add a line to export the App component as the default export of the module.
React
Hint
Use export default App; at the end of the file.
Practice
(1/5)
1. What is the main purpose of an Error Boundary in React?
easy
A. To catch JavaScript errors in child components and display a fallback UI
B. To manage state across multiple components
C. To optimize rendering performance
D. To handle user input validation
Solution
Step 1: Understand Error Boundary role
Error boundaries catch errors in the components below them in the tree to prevent the whole app from crashing.
Step 2: Identify the correct purpose
They show a fallback UI when an error happens, improving user experience.
Final Answer:
To catch JavaScript errors in child components and display a fallback UI -> Option A
Quick Check:
Error boundaries catch errors = C [OK]
Hint: Error boundaries catch errors and show fallback UI [OK]
Common Mistakes:
Confusing error boundaries with state management
Thinking error boundaries optimize rendering
Assuming error boundaries handle input validation
2. Which of the following is the correct way to define an Error Boundary component in React?
easy
A. A class component implementing componentDidCatch and getDerivedStateFromError
B. A functional component using useErrorBoundary hook
C. A class component implementing componentDidMount and shouldComponentUpdate
D. A functional component with useEffect to catch errors
Solution
Step 1: Recall Error Boundary requirements
Error boundaries must be class components that implement componentDidCatch and getDerivedStateFromError lifecycle methods.
Step 2: Identify correct lifecycle methods
Only A class component implementing componentDidCatch and getDerivedStateFromError lists these two required methods correctly.
Final Answer:
A class component implementing componentDidCatch and getDerivedStateFromError -> Option A
Quick Check:
Class + componentDidCatch + getDerivedStateFromError = A [OK]
Hint: Error boundaries are class components with special lifecycle methods [OK]
Common Mistakes:
Thinking functional components can be error boundaries
Confusing lifecycle methods needed for error boundaries
Using hooks that don't exist for error boundaries
3. Given this Error Boundary class component, what will be rendered if ChildComponent throws an error?