Sharpen your frontend development skills with this quiz on Error Boundaries in React, designed to help you understand error handling, lifecycle methods, and best practices for building resilient user interfaces using error boundaries.
Which of the following best describes an error boundary in React?
Explanation: An error boundary is a React component that catches JavaScript errors anywhere in its child component tree, logs them, and displays a fallback UI instead of crashing the entire app. It does not deal with authentication, so the answer about user access is incorrect. A CSS class doesn't catch errors or show fallbacks. Tracking API errors in network requests is not the primary role of error boundaries.
Which React lifecycle method must a class component implement to act as an error boundary?
Explanation: The componentDidCatch lifecycle method is required for a class component to function as an error boundary. It allows the component to catch errors from child components. componentDidUpdate and getDerivedStateFromProps do not handle errors. shouldComponentUpdate controls rendering behavior, not error handling.
Can you implement an error boundary using a function component in React?
Explanation: As of now, error boundaries can only be implemented with class components because only they support lifecycle methods like componentDidCatch. Function components cannot directly be used as error boundaries, and the useEffect hook or try-catch in function bodies does not achieve the same result.
Which types of errors can error boundaries catch in React?
Explanation: Error boundaries catch errors that happen during rendering, in lifecycle methods, and constructors of child components. They do not handle network request issues, compilation errors, or errors in event handlers. Event handler errors are not caught because they occur outside of rendering or lifecycle methods.
When an error boundary catches an error, what is typically rendered?
Explanation: Error boundaries display a fallback UI component defined by the developer to inform users of the error gracefully. They do not show alert popups by default, nor do they reload the whole page. The broken component should not continue to display once an error is caught.
Where should you place error boundaries in a React application for optimal user experience?
Explanation: It is recommended to wrap potentially unstable components with error boundaries, so if a part fails, the rest of the UI stays intact. Placing an error boundary only at the app root may hide where the error came from. Wrapping every component is unnecessary, and wrapping style tags serves no purpose.
How should you handle errors that occur inside event handler functions, such as onClick, in React?
Explanation: Errors thrown in event handlers are not caught by error boundaries. Instead, developers need to use try-catch within the event handler function to manage such errors. Error boundaries and extra wrappers do not catch these runtime errors. The statement that errors cannot be handled at all is incorrect as try-catch provides a way.
What action commonly resets the error state of an error boundary component to allow re-rendering?
Explanation: Changing the key prop on the error boundary causes it to remount, clearing any previously captured errors and allowing re-rendering. Reloading the browser refreshes the entire app, which is unnecessary. setError is not a standard method, and importing again has no effect.
If an error occurs in a child component, what happens to sibling components not wrapped by the same error boundary?
Explanation: Only the descendants of an error boundary are affected when an error is caught; sibling components outside the error boundary are not impacted and continue rendering. Siblings are not hidden or wrapped in the fallback UI. Errors do not cascade to siblings unless wrapped together.
Which two methods are required to create a working error boundary class component in React?
Explanation: A valid error boundary must implement a render method to display content and componentDidCatch to catch errors. The other options list methods not related to error boundaries. componentDidMount initializes logic but does not catch errors. constructor and other lifecycle methods are optional, not required.
What is the purpose of the getDerivedStateFromError static method in error boundaries?
Explanation: getDerivedStateFromError allows updating the state when an error is thrown, which then triggers rendering the fallback UI. It is not used for data fetching, API error handling, or scheduling state updates after changes. The other options do not describe its error handling purpose.
From which major version of React are error boundaries officially supported?
Explanation: Error boundaries were introduced in React version 16.0, making it the first version with official support. Versions 13.0 and 15.5 did not have this feature, and 17.2 is a later version with error boundaries already established.
What is a typical use for the error and info arguments received by componentDidCatch in an error boundary?
Explanation: componentDidCatch often logs errors and additional info to an external service for diagnosis. It does not generate CSS, update unrelated state, or automatically fix bugs. The main purpose is to capture and report errors, not resolve them directly.
What happens if an error is not caught by any error boundary in a React tree?
Explanation: If an error isn't caught by an error boundary, React unmounts the entire component tree below the component that threw the error, possibly causing a blank screen. Errors are not ignored, retried, or self-limited to the failing component without error boundaries.
How can you display different fallback UIs for different sections in a React application with error boundaries?
Explanation: Wrapping each section in its own error boundary allows customized fallback UIs for different areas. Global CSS doesn't control error boundary content, and putting a fallback in index.html is not reactive to app errors. A single boundary for the whole app limits customization.
Select a scenario where React error boundaries are unable to catch an error.
Explanation: Error boundaries do not catch errors thrown in event handlers like onClick, which need separate handling. They do catch errors thrown during rendering, in constructors, and in lifecycle methods of child components, making those choices incorrect for this scenario.