score:7
You can't do that with hooks. Hooks do not have an equivalent of componentDidCatch. See this section of the hook FAQ
score:7
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.
score:11
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
score:14
*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.
score:38
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.
Source: stackoverflow.com
Related Query
- How to implement Error Boundary with React Hooks Component
- How to implement error boundary in react with server side rendering in nextjs?
- How to implement componentDidMount with hooks in React to be in line with the EsLint rule "react-hooks/exhaustive-deps": "warn"?
- How to load child component with props on first render with React Hooks
- Why React still render the child component with Error Boundary
- How to re-render component with react hooks
- How to structure a component using React Hooks with an object that uses the DOM directly? (such as OpenLayers)?)
- How to implement a React Component with sub-components and call them?
- How to include Preact component with hooks in React app?
- How to convert React class component to functional component with hooks
- How to test a handle function call with a react functional component with hooks
- How do I 'reset' a React component after a Mutation error with react-apollo?
- how to trigger an event after component did mount with react hooks
- Error with calling React Hooks inside of Function Component
- How to convert a React class component to a function component with hooks to get firebase data
- Jest Unit Test for React Hooks component with act() error
- How to allow fallback prop in React error boundary with Typescript?
- React hooks & Context: Error when using context inside a child component with useEffect
- React hooks component not updating after map, but see data loading / refreshing with error
- How do you export react library component with hooks to use in html file?
- React Tooltip component with TypeScript getting a 'Possibly Undefined' error on my useRef element, not sure how to fix
- How to render a different component with React Hooks
- How to use React error boundary with promises
- How to test React component that uses React Hooks useHistory hook with Enzyme?
- How to test react component with hooks using react testing library
- How to pass props from one component to another along with props validation - React Hooks
- Using use-State hook in react function always throws an error + How to share a variable with another component
- How to toggle Sidebar Component in Header with a Burger Menu using Hooks in React
- How to force refresh of GrandParent component from grandchild component with React hooks in SPFx
- how to validate username minlength and show error message with react hooks
More Query from same tag
- Hide tabs on Login screen in Ionic React
- Don't let flex to wrap label text of toggle
- Setting background image in react using inline CSS based on react logic
- fit text inside an element with fixed width
- deploying nextjs app to elastic beanstalk
- Antd Filter dropdown doesn't render in react typescript
- link login page to homepage using react
- skip assigning value to const
- How to type optionally empty objects
- React-router's history in react-redux provider's connect function
- Having issues sending props from a api call into another react component
- array value is undefined … how do I test for that
- UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'amount' of undefined
- React w/ Service Worker & Push Notifications
- how can i create Shopify APP that interact with user from product detail page
- How can update the DOM if the state changes?
- Trying to iterate over a JSON object in React
- React – expose a components functions to its children
- Getting dotted line on html Canvas
- React prevent button in form to be clicked on hitting return
- Global theme with TypeScript/React/Material-UI
- Getting map data using React from firestore
- Is it possible to set the Analytics ID in gatsby-node.js?
- React.js changing Chart.js type by selected radio
- Component not receiving props when pushed from same path - react router
- Element type is invalid: expected a string (for built-in components) or a class/function React Error
- Using Nightmare web-scraping with React (create-react-app)
- How can I detect the URL is mp4 or m3u8 and play it on browser?
- Spring security for REST api shows the browser popup when auth fails
- How to pass data using useContext hook?