Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Error Boundary with React Hooks Component

Tags:

reactjs

How can we implement error Boundary in React Hooks. Is it even supported with react Hooks?

like image 568
Harish Dhami Avatar asked Mar 05 '20 03:03

Harish Dhami


People also ask

How are error boundaries used in React Hooks?

A class component becomes an error boundary if it defines either (or both) of the lifecycle methods static getDerivedStateFromError() or componentDidCatch() . Use static getDerivedStateFromError() to render a fallback UI after an error has been thrown. Use componentDidCatch() to log error information.

How do you trigger an error boundary React?

With the new feature in React, developers can test the Error Boundaries by a toggle error button, added to the DevTools. When we click on the toggle error button on the component labeled 'Inner Component', Error boundary is triggered.

Where does React error boundary go?

Now that Error Boundaries are available since React version 16, it's generally advisable to use at least one Error Boundary at the root of your app (e.g., the App. js file). This will prevent users from seeing a blank HTML page and perhaps see a nice fallback UI instead.


5 Answers

The questions is whether it is possible to implement Error Boundary as a hook and the answer is no BUT it does not mean that you can not use Error Boundary class components in a project which you use hooks heavily.

Create a Error Boundary class component and wrap your React Functional Components(hooks) with the Error Boundary class component.

like image 178
Efe Avatar answered Oct 04 '22 21:10

Efe


*There is no Error Boundaries in hook yet *

componentDidCatch 
and 
getDerivedStateFromError 

There are no Hook equivalents for these methods yet, but they will be added soon.

like image 39
Asgar Ali Khachay Avatar answered Oct 04 '22 21:10

Asgar Ali Khachay


You can implement error boundary in react hooks with the help of react-error-boundary package.

npm install --save react-error-boundary

Then:

import {ErrorBoundary} from 'react-error-boundary'

function ErrorFallback({error, resetErrorBoundary}) {
  return (
    <div role="alert">
      <p>Something went wrong:</p>
      <pre>{error.message}</pre>
      <button onClick={resetErrorBoundary}>Try again</button>
    </div>
  )
}

const ui = (
  <ErrorBoundary
    FallbackComponent={ErrorFallback}
    onReset={() => {
      // reset the state of your app so the error doesn't happen again
    }}
  >
    <ComponentThatMayError />
  </ErrorBoundary>
)

Please read more on React-error-boundary

like image 22
Victor Karangwa Avatar answered Oct 04 '22 21:10

Victor Karangwa


I wrote react-use-error-boundary to achieve this.

React 16 introduced Error Boundaries. As of React 17, there still is not an equivalent hook for function components.

I liked the API implemented by Preact's useErrorBoundary and attempted to recreate a similar API. If you're interested in building this from scratch you can checkout the code on GitHub.

like image 40
Tate Thurston Avatar answered Oct 04 '22 21:10

Tate Thurston


You can't do that with hooks. Hooks do not have an equivalent of componentDidCatch. See this section of the hook FAQ

like image 33
Nicholas Tower Avatar answered Oct 04 '22 21:10

Nicholas Tower