Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

use-error-boundary

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-error-boundary

React hook for using error boundaries

  • 2.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
18K
decreased by-26.45%
Maintainers
1
Weekly downloads
 
Created
Source

use-error-boundary

npm version build status license

A react hook for using error boundaries in your functional components.

It lets you keep track of the error state of child components, by wrapping them in a provided ErrorBoundary component.

:warning: Read more about error boundaries and their intended use in the React documentation, this will only catch errors when rendering your children!

Installation

npm i use-error-boundary

Breaking changes in 2.x

If you are upgrading from version 1.x please make sure you are not using the errorInfo object. The hook itself and the renderError callback no longer provide this object.

For advanced use, please refer to Custom handling of error and errorInfo.

Examples and usage

Import the hook:

// Named
import { useErrorBoundary } from "use-error-boundary"
// Default
import useErrorBoundary from "use-error-boundary"

Please read more info on the returned properties by the hook.

const MyComponent = () => {

  const {
    ErrorBoundary,
    didCatch,
    error
  } = useErrorBoundary()

  ...

}

Use without render props

Wrap your components in the provided ErrorBoundary, if it catches an error the hook provides you with the changed state and the boundary Component will render nothing. So you have to handle rendering some error display yourself.

If you want the boundary to also render your error display, you can use it with render props

const JustRenderMe = () => {
  throw new Error("💥")
}

const MyComponent = () => {
  const { ErrorBoundary, didCatch, error } = useErrorBoundary()

  return (
    <>
      {didCatch ? (
        <p>An error has been catched: {error.message}</p>
      ) : (
        <ErrorBoundary>
          <JustRenderMe />
        </ErrorBoundary>
      )}
    </>
  )
}

Use with render props

Optionally, you can pass a render and renderError function to render the components to display errors in the boundary itself:

/**
 * The renderError function also passes the error and errorInfo, so that you can display it using
 * render props.
 */
return (
  <ErrorBoundary
    render={() => <SomeChild />}
    renderError={({ error }) => <MyErrorComponent error={error} />}
  />
)

Custom handling of error and errorInfo

The hook now accepts an options object that you can pass a onDidCatch callback that gets called when the ErrorBoundary catches an error.

useErrorBoundary({
  onDidCatch: (error, errorInfo) => {
    // For logging/reporting
  },
})

Returned Properties

These are the properties of the returned Object:

PropertyTypeDescription
ErrorBoundaryReact ComponentSpecial error boundary component that provides state changes to the hook.
:warning: You need to use this as the error boundary! Otherwise, the state will not update when errors are catched!
The ErrorBoundary is guaranteed referential equality across rerenders.
didCatchBooleantrue if an error has been catched
errorError Object or nullThe error catched by the Boundary

If you are searching for the errorInfo property, please read Breaking Changes in 2.x.

Why should I use this?

React does not provide a way to catch errors within the same functional component and you have to handle that in a class Component with special lifecycle methods. If you are new to ErrorBoundaries, I recommend implementing this yourself!

This packages purpose is to provide an easy drop in replacement for projects that are being migrated to hooks and to pull the error presentation out of the boundary itself by putting it on the same level you are catching the errors.

Contributing

Contributions are always welcome.

Feel free to open issues or pull requests!

Keywords

FAQs

Package last updated on 25 Sep 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc