UNPKG

@nodeject/ui-components

Version:

UI library for non-trivial components

50 lines (49 loc) 2.18 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import * as React from 'react'; var ErrorBoundary = /** @class */ (function (_super) { __extends(ErrorBoundary, _super); function ErrorBoundary() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.state = { error: null, errorInfo: null, hasError: false }; return _this; } ErrorBoundary.getDerivedStateFromError = function (error) { // Update state so the next render will show the fallback UI. return { hasError: true }; }; ErrorBoundary.prototype.componentDidCatch = function (error, errorInfo) { // Catch errors in any components below and re-render with error message this.setState({ error: error, errorInfo: errorInfo }); // You can also log error messages to an error reporting service here }; ErrorBoundary.prototype.render = function () { if (this.state.errorInfo) { // Error path return (React.createElement("div", null, React.createElement("h2", null, "Something went wrong."), React.createElement("details", { style: { whiteSpace: 'pre-wrap' } }, this.state.error && this.state.error.toString(), React.createElement("br", null), this.state.errorInfo.componentStack))); } // Normally, just render children return this.props.children; }; return ErrorBoundary; }(React.Component)); export { ErrorBoundary };