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

  • 1.1.1
  • 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.

Installation

npm i use-error-boundary

Examples and usage

Import the hook:

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

Use the hook in your react component, it provides you with this object:

const MyComponent = () => {

  const {
    ErrorBoundary, // class - The react component to wrap your children in. This WILL NOT CHANGE
    didCatch, // boolean - Whether the ErrorBoundary catched something
    error, // null or the error
    errorInfo // null or the error info as described in the react docs
  } = useErrorBoundary()

  ...

}

Wrap your components in the ErrorBoundary:


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

const MyComponent = () => {

  ...

  /**
   * The ErrorBoundary renders its children directly,
   * when a component throws, the ErrorBoundary will return null from its render method.
   */
  return (
    <ErrorBoundary>
      <JustRenderMe />
    </ErrorBoundary>
  )
}

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, so that you can display it using
 * render props.
 */
return (
  <ErrorBoundary
    render={() => <SomeChild />}
    renderError={({ error }) => <MyErrorComponent error={error} />}
  />
)

TODO

  • Extend default ErrorBoundary component to render a provided component when there is an error
  • Passing own component as ErrorBoundary and wrap it
  • Change createErrorBoundaryClass createWrappedErrorBoundary to pass onDidCatch as prop (HOC)
  • Cleanup tests
  • Comment tests

Contributing

Contributions are welcome, as this is my first properly published npm package.

Feel free to open issues or pull requests! I will review them as fast as possible.

Keywords

FAQs

Package last updated on 12 Jan 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