I have a simple component that acts as an error boundary in my React app, and which passes off any errors to a logging service.
It looks something like this:
export class CatchError extends React.PureComponent { state = { hasError: false } componentDidCatch(error, info) { this.props.log({ error, info }) this.setState({ hasError: true }) } render() { const child = typeof this.props.children === "function" ? this.props.children({ error: hasError }) : children return React.cloneElement(child, { error: this.state.hasError }) } }
Is there a React hook equivalent to componentDidCatch
so I can make this component a function and not a class?
So it might look something like this:
export function CatchError({ children, log }) { const [hasError, setHasError] = useState(false) const caught = useDidCatch() useEffect(() => { const [error, info] = caught log({ error, info }) setHasError(true) }, [caught]) const child = typeof children === "function" ? children({ error: hasError }) : children return React.cloneElement(child, { error: hasError }) }
You can hack the useMemo hook to imitate a componentWillMount lifecycle event. Just do: const Component = () => { useMemo(() => { // componentWillMount events },[]); useEffect(() => { // componentDidMount events return () => { // componentWillUnmount events } }, []); };
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.
Hooks were designed to replace class and provide another great alternative to compose behavior into your components. Higher Order Components are also useful for composing behavior.
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.
There is not a React hook equivalent of componentDidCatch
.
However, the React team plans to add one soon.
The React docs state:
There are no Hook equivalents to the uncommon getSnapshotBeforeUpdate and componentDidCatch lifecycles yet, but we plan to add them soon.
Read more: https://reactjs.org/docs/hooks-faq.html#do-hooks-cover-all-use-cases-for-classes
There is not any official hook for Error Boundaries(componentDidCatch) in react. but you can simply use try...catch to catch component errors like this:
function myComponent(...) { // Initialize state variables. // Initialize context variables. // Initialize custom hooks. // ... try { // Define internal functions. // Define internal variables. return ( <SomeThing /> ) } catch (error) { // Catch internal functions, variables and return (jsx) errors. // You can also create a lib to log the error to an error reporting service // and use it here. } }
The try...catch
block is the Error Boundry here.
Limitation: when you use a hook like useEffect
and you use some internal functions
in it, you cannot put that internal function into try...catch(Error Boundary) block because you should define that function on top of useEffect hook (why?) and you shouldn't use useEffect conditionally (why?)!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With