Skip to content

Instantly share code, notes, and snippets.

@andywer
Last active March 7, 2024 05:52
Show Gist options
  • Save andywer/800f3f25ce3698e8f8b5f1e79fed5c9c to your computer and use it in GitHub Desktop.
Save andywer/800f3f25ce3698e8f8b5f1e79fed5c9c to your computer and use it in GitHub Desktop.
React - Functional error boundaries

React - Functional error boundaries

Thanks to React hooks you have now happily turned all your classes into functional components.

Wait, all your components? Not quite. There is one thing that can still only be implemented using classes: Error boundaries.

There is just no functional equivalent for componentDidCatch and deriveStateFromError yet.

Proposed solution

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>
  }
})

API

type ErrorHandler = (error: Error, info: React.ErrorInfo) => void

Catch(component: React.ComponentType)

Catch(component: React.ComponentType, errorHandler: ErrorHandler)

Wraps the functional component component to make it an error boundary. The caught error is passed to the component function as a second parameter.

The optional second argument to Catch() (errorHandler) is a function that acts as componentDidCatch(). Use it to handle side effects like error logging.

Implementation

Find the code below.

Outlook

Would be great to see something like this integrated into the React.js core library:

import React from "react"

type Props = {
  children: React.ReactNode
}

const MyErrorBoundary = React.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>
  }
})
import React from "react"
type ErrorHandler = (error: Error, info: React.ErrorInfo) => void
type ErrorHandlingComponent<Props> = (props: Props, error?: Error) => React.ReactNode
type ErrorState = { error?: Error }
export default function Catch<Props extends {}>(
component: ErrorHandlingComponent<Props>,
errorHandler?: ErrorHandler
): React.ComponentType<Props> {
return class extends React.Component<Props, ErrorState> {
state: ErrorState = {
error: undefined
}
static getDerivedStateFromError(error: Error) {
return { error }
}
componentDidCatch(error: Error, info: React.ErrorInfo) {
if (errorHandler) {
errorHandler(error, info)
}
}
render() {
return component(this.props, this.state.error)
}
}
}
@asifsaho
Copy link

asifsaho commented Aug 5, 2020

Should I turn this little gist into a package? Seems to have finally gained a little bit of traction.

Yes please, I also need this. Although I couldn't make the above gists work yet.

Copy link

ghost commented Aug 5, 2020

Should I turn this little gist into a package? Seems to have finally gained a little bit of traction.

Have you already seen this - https://www.npmjs.com/package/react-error-boundary ?

Kent Dodds wrote an article on this - https://kentcdodds.com/blog/use-react-error-boundary-to-handle-errors-in-react

I really thought initially you had written this library because when you wrote this gist, I didn't find any such library.

The implementation also seems fairly close, thats why I was surprised.

@andywer
Copy link
Author

andywer commented Aug 6, 2020

@sriram-ethos No, I hadn't seen that before! I googled for something like this back then when I wrote this gist, but definitely didn't see that…

Yeah, I guess there's only so many ways to write such a component and stick to existing API patterns 🙂

Copy link

ghost commented Aug 6, 2020

Yeah, I guess there's only so many ways to write such a component and stick to existing API patterns 🙂

🙂

Copy link

ghost commented Nov 10, 2020

I really see no point in exporting a function when ultimately all I'll be doing will be cloaking a class instance inside a function. Why not just use the class directly? Wouldn't hurt to have a single class component rather than going through all this complexity, don't you think?

@VicJerUk
Copy link

VicJerUk commented Sep 20, 2021

I really see no point in exporting a function when ultimately all I'll be doing will be cloaking a class instance inside a function. Why not just use the class directly? Wouldn't hurt to have a single class component rather than going through all this complexity, don't you think?

Absolutely. I can't see the issue here with just using the class component for ErrorBoundary.

@manakupadhyay
Copy link

@andywer Could you please show an example of how we can use it?

@andywer
Copy link
Author

andywer commented May 14, 2022

@manakupadhyay It can be used as in the example at the top, under "Outlook", just that it's not part of React:

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>
  }
})

and then

function MyComponent() {
  return (
    <MyErrorBoundary>
      <ContentThatMightThrow>
    </MyErrorBoundary>
  );
}

@manakupadhyay
Copy link

@andywer Thanks a lot :)

@ABHISHEK-KEDAR-21
Copy link

Well I am not a fan of functional components and stick to class based components but Thanks anyway for writing this.

Copy link

ghost commented Jul 28, 2023

i tried it works great how but it should reset the state of error if click on something and it takes me to a new route how can i do that i tried to see where can i do it but couldnt find any appropriate place

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment