Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make use of Error boundaries in functional React components?

I can make a class an error boundary in React by implementing componentDidCatch.

Is there a clean approach to making a functional component into an error boundary without converting it into a class?

Or is this a code smell?

like image 779
Dancrumb Avatar asked Jan 28 '18 02:01

Dancrumb


People also ask

Can I use error boundary in functional component?

Only class components can be error boundaries. In practice, most of the time you'll want to declare an error boundary component once and use it throughout your application. Note that error boundaries only catch errors in the components below them in the tree. An error boundary can't catch an error within itself.

How do you implement error boundaries in functional components?

Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them. In order to use Error Boundary in Functional Component, I use react-error-boundary. When we run this application, we will get a nice error display form the content of ErrorHandler component.

How do you use React error boundaries?

So basically, error boundaries only handle errors in the parts of our code that involve React. To create an error boundary, we simply have to create a class component and define a state variable for determining whether the error boundary has caught an error.

How do you handle errors in React components?

Error handling with Error Boundaries — For class components. Error boundaries are the most straightforward and effective way to handle errors that occur within your React components. You can create an error boundary component by including the life cycle method componentDidCatch(error, info) if you use class component.


2 Answers

As of v16.2.0, there's no way to turn a functional component into an error boundary.

The React docs are clear about that, although you're free to reuse them as many times as you wish:

The componentDidCatch() method works like a JavaScript catch {} block, but for components. Only class components can be error boundaries. In practice, most of the time you’ll want to declare an error boundary component once and use it throughout your application.

Also bear in mind that try/catch blocks won't work on all cases.
If a component deep in the hierarchy tries to updates and fails, the try/catch block in one of the parents won't work -- because it isn't necessarily updating together with the child.

like image 124
gustavohenke Avatar answered Sep 22 '22 23:09

gustavohenke


There is an implementation that can handle with non-existent functionalities for a functional component such as componentDidCatch and deriveStateFromError.

According to the author, it is based on React.memo().

The proposed solution is greatly inspired by the new React.memo() API.

import Catch from "./functional-error-boundary"  type Props = {   children: React.ReactNode }  const MyErrorBoundary = Catch(function MyErrorBoundary(props: Props, error?: Error) {   if (error) {     return (       <div className="error-screen">         <h2>An error has occured</h2>         <h4>{error.message}</h4>       </div>     )   } else {     return <React.Fragment>{props.children}</React.Fragment>   } }) 

reference and API here

like image 33
Luis Febro Avatar answered Sep 21 '22 23:09

Luis Febro