4. What is wrong with this ErrorBoundary component code?
export function ErrorBoundary(error) {
return <div>Error: {error.message}</div>;
}
medium
A. The error parameter should be destructured as { error }, but here it is a plain parameter
B. The error parameter should be destructured as { error }
C. The error parameter should be destructured as { error } but it is not
D. The component must return null on error
Solution
Step 1: Check parameter syntax
ErrorBoundary receives props as an object, so the parameter should be destructured like { error }, not a plain variable.
Step 2: Identify correct destructuring
Using error directly means it is the whole props object, so error.message will be undefined or cause error.
Final Answer:
The error parameter should be destructured as { error }, but here it is a plain parameter -> Option A
Quick Check:
Destructure props as { error } in ErrorBoundary [OK]
Hint: Destructure error prop: use ({ error }) not (error) [OK]
Common Mistakes:
Using plain parameter instead of destructuring
Returning null instead of fallback UI
Confusing error prop with other props
5. You want to create an ErrorBoundary that shows a custom message for network errors and a generic message for others. Which code correctly implements this behavior?
export function ErrorBoundary({ error }) {
if (error.message.includes('Network')) {
return <div>Network error occurred. Please try again.</div>;
}
return <div>Something went wrong.</div>;
}
hard
A. The error prop should be named props, not error
B. This code will crash if error.message is undefined
C. The if condition should check error.type, not error.message
D. This code correctly shows a custom message for network errors and generic for others
Solution
Step 1: Analyze error message check
The code checks if error.message contains 'Network' to show a specific message.
Step 2: Verify fallback message
If the error is not a network error, it shows a generic fallback message, which is correct behavior.
Final Answer:
This code correctly shows a custom message for network errors and generic for others -> Option D
Quick Check:
Conditional error messages based on error.message = C [OK]
Hint: Check error.message content to customize fallback UI [OK]
Common Mistakes:
Assuming error.message can be undefined without check
Using wrong prop name instead of error
Checking wrong error property like error.type
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.