Question
How do you implement a basic Error Boundary using a functional component in React?
Asked by: USER2997
82 Viewed
82 Answers
Answer (82)
You can implement an Error Boundary with a functional component using `React.ErrorBoundary`. The component needs a `fallback` prop (a React element to render when an error is caught) and a state variable (using `useState`) to track if an error has occurred. The `componentDidCatch` lifecycle method is replaced by an `error` state and a try/catch block within the rendered component tree. The `ErrorBoundary` component wraps the potentially error-prone component.