All Projects → JoschuaSchneider → Use Error Boundary

JoschuaSchneider / Use Error Boundary

Licence: mit
React hook for using error boundaries in your functional components

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Labels

Projects that are alternatives of or similar to Use Error Boundary

Npm Link Up
🔄 Link your NPM projects automatically, for sophisticated / modular local development.
Stars: ✭ 68 (-18.07%)
Mutual labels:  npm
Have It
The fastest NPM install does nothing because you already have it
Stars: ✭ 75 (-9.64%)
Mutual labels:  npm
Xa
Beautiful & Customizable logger ❤️
Stars: ✭ 78 (-6.02%)
Mutual labels:  npm
Alpine Phoenix Builder
Up to date Alpine image with the latest language versions for staged Elixir and Phoenix builds.
Stars: ✭ 71 (-14.46%)
Mutual labels:  npm
Smart Buffer
smart-buffer is a Buffer wrapper that adds automatic read & write offset tracking, string operations, data insertions, and more.
Stars: ✭ 73 (-12.05%)
Mutual labels:  npm
Incompose
A inferno utility belt for function components and higher-order components
Stars: ✭ 76 (-8.43%)
Mutual labels:  npm
Node Installed Check
Checks that all dependencies in your package.json have supported versions installed and complies with your specified node engine version range
Stars: ✭ 67 (-19.28%)
Mutual labels:  npm
Npm Rails
NPM support for Rails with Bundler-like DSL
Stars: ✭ 81 (-2.41%)
Mutual labels:  npm
Mockingcase
node package that converts a string to mOcKiNgCaSe
Stars: ✭ 74 (-10.84%)
Mutual labels:  npm
Minimal Feedback
🗳 minimal-feedback is a blazingly fast and highly customizable component to get user feedback.
Stars: ✭ 78 (-6.02%)
Mutual labels:  npm
Ni
📦A better npm install
Stars: ✭ 72 (-13.25%)
Mutual labels:  npm
Dicomweb Client
DICOMweb client side JavaScript implementation
Stars: ✭ 73 (-12.05%)
Mutual labels:  npm
Emma Cli
📦 Terminal assistant to find and install node packages.
Stars: ✭ 1,201 (+1346.99%)
Mutual labels:  npm
Ssri
Standard Subresource Integrity library for Node.js
Stars: ✭ 69 (-16.87%)
Mutual labels:  npm
Webcam Easy
javascript access webcam stream and take photo
Stars: ✭ 79 (-4.82%)
Mutual labels:  npm
Npm Api
Node.js library for getting info from NPM’s API
Stars: ✭ 67 (-19.28%)
Mutual labels:  npm
Bump
🌱 Sublime Text 3 Plugin to keep package.json dependencies fresh.
Stars: ✭ 76 (-8.43%)
Mutual labels:  npm
Node Developer Boilerplate
🍭 Boilerplate for ES6+ Node.js and npm Developer
Stars: ✭ 82 (-1.2%)
Mutual labels:  npm
Enseada
A Cloud native multi-package registry
Stars: ✭ 80 (-3.61%)
Mutual labels:  npm
React Image Timeline
📆 An image-centric timeline component for React.js
Stars: ✭ 77 (-7.23%)
Mutual labels:  npm

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.

⚠️ 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:

Property Type Description
ErrorBoundary React Component Special error boundary component that provides state changes to the hook.
⚠️ 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.
didCatch Boolean true if an error has been catched
error Error Object or null The 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!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].