UNPKG

zent

Version:

一套前端设计语言和基于React的实现

48 lines (47 loc) 1.71 kB
import { __extends } from "tslib"; import { jsx as _jsx } from "react/jsx-runtime"; import { Component } from 'react'; import ErrorBoundaryFallbackComponent from './Fallback'; import catchError from './catchError'; import withErrorBoundary from './withErrorBoundary'; function getComponentStack(info) { return info ? info.componentStack : ''; } var ErrorBoundary = (function (_super) { __extends(ErrorBoundary, _super); function ErrorBoundary() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.state = { error: null, info: null, }; return _this; } ErrorBoundary.prototype.componentDidCatch = function (error, info) { var onError = this.props.onError; if (typeof onError === 'function') { try { onError(error, getComponentStack(info)); } catch (ignoredError) { } } this.setState({ error: error, info: info }); }; ErrorBoundary.prototype.render = function () { var _a = this.props, children = _a.children, FallbackComponent = _a.FallbackComponent; var _b = this.state, error = _b.error, info = _b.info; if (error !== null) { return (_jsx(FallbackComponent, { componentStack: getComponentStack(info), error: error }, void 0)); } return children; }; ErrorBoundary.defaultProps = { FallbackComponent: ErrorBoundaryFallbackComponent, }; ErrorBoundary.withErrorBoundary = withErrorBoundary; ErrorBoundary.catchError = catchError; return ErrorBoundary; }(Component)); export { ErrorBoundary }; export default ErrorBoundary;