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 - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

2

package.json
{
"name": "use-error-boundary",
"version": "1.0.3",
"version": "1.0.4",
"description": "React hook for using error boundaries",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

# use-error-boundary
![npm version](https://img.shields.io/npm/v/use-error-boundary.svg)

@@ -6,6 +7,7 @@ ![build status](https://travis-ci.org/JoschuaSchneider/use-error-boundary.svg?branch=master)

This react hook provides a ErrorBoundary class and the error state of the error boundary.
A **react hook** for using error boundaries in your functional components.
The ErrorBoundary is implemented using classes (as there is no hook-based componentDidCatch implementation yet).
It lets you keep track of the error state of child components, by wrapping them in a provided `ErrorBoundary` component.
### Installation

@@ -17,23 +19,51 @@

### Functionality
## Examples and usage
The hook provides an `ErrorBoundary` class, that accepts children and renders them.
In case of an exception, the `ErrorBoundary` will render `null` and the hook will update its state.
Import the hook:
```javascript
// Named
import { useErrorBoundary } from 'use-error-boundary'
// Default
import useErrorBoundary from 'use-error-boundary'
```
This lets you implement an error screen depending on the Status of the Error boundary, using function components and hooks!
Use the hook in your react component,
it provides you with this object:
### Usage
```javascript
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`:
```javascript
import React from "react"
import { useErrorBoundary } from "use-error-boundary"
const myComponent = () => {
const { ErrorBoundary, didCatch, error, errorInfo } = useErrorBoundary()
const JustRenderMe = () => {
throw new Error('💥')
}
if (didCatch) return <h1>Something went wrong</h1>
const MyComponent = () => {
...
/**
* The ErrorBoundary renders its children directly,
* when a component throws, the ErrorBoundary will return null from its render method.
*
* See TODO section :)
*/
return (
<ErrorBoundary>
<SomeChildren />
<JustRenderMe />
</ErrorBoundary>

@@ -44,2 +74,9 @@ )

## 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` to pass `onDidCatch` as prop (HOC)
- [ ] Cleanup tests
## Contributing

@@ -46,0 +83,0 @@

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