Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Add createErrorBoundary HOC
Browse files Browse the repository at this point in the history
  • Loading branch information
octet-stream committed Jan 13, 2020
1 parent 90eba54 commit 9cf39f6
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/lib/hoc/error.jsx
@@ -0,0 +1,43 @@
import {createElement, Component} from "react"
import {node} from "prop-types"

import getName from "lib/helper/component/getName"

function createErrorBoundary(Target) {
class Error extends Component {
static displayName = `Error(${getName(Target)})`

static propTypes = {
children: node.isRequired
}

static getDerivedStateFromError = error => ({error})

componentDidCatch(error, info) {
console.error(error)

if (process.env.NODE_ENV !== "production" && info) {
console.error(info)
}
}

render() {
const {error} = this.state

if (!error) {
return this.props.children
}

// Throw suspenders further
if (error instanceof Promise) {
throw error
}

return createElement(Target, {error})
}
}

return Error
}

export default createErrorBoundary

0 comments on commit 9cf39f6

Please sign in to comment.