UNPKG

respond-framework

Version:
59 lines (58 loc) 1.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var React = require("react"); var _utils = require("../utils.js"); class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { error: null }; } static getDerivedStateFromError(error) { return { error }; } async componentDidCatch(error, errorInfo) { await this.props.state.respond.onError({ error, kind: 'render' }); } render() { const { error } = this.state; const { Error = DefaultError, children, state } = this.props; const clear = () => this.setState({ error: null }); const props = { children, error, clear, state }; return !error ? children : /*#__PURE__*/React.createElement(Error, props); } } exports.default = ErrorBoundary; const DefaultError = ({ children, error, clear, state }) => { if (_utils.isProd) { console.warn('respond: supply an `Error` component in your top module, or as a prop to `render({ Error })` in production'); } return children; };