UNPKG

@mskcc/carbon-react

Version:

Carbon react components for the MSKCC DSM

72 lines (64 loc) 2.37 kB
/** * MSKCC 2021, 2024 */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js'); var React = require('react'); var PropTypes = require('prop-types'); var ErrorBoundaryContext = require('./ErrorBoundaryContext.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var React__default = /*#__PURE__*/_interopDefaultLegacy(React); var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes); /** * React introduced additional lifecycle methods in v16 for capturing errors * that occur in a specific sub-tree of components. This component helps to * consolidate some of the duplication that occurs when using these lifecycle * methods across a codebase. In addition, it allows you to specify the fallback * UI to display when an error occurs in the sub-tree through the `fallback` * prop. * * This component roughly follows the React.js docs example code for these * methods. In addition, it takes advantage of an `ErrorBoundaryContext` so that * consumers can specify their own logic for logging errors. For example, * reporting an error in the UI to an external service for every `ErrorBoundary` * used. * * Reference: * https://reactjs.org/docs/error-boundaries.html#introducing-error-boundaries */ class ErrorBoundary extends React__default["default"].Component { constructor() { super(...arguments); _rollupPluginBabelHelpers.defineProperty(this, "state", { hasError: false }); } static getDerivedStateFromError() { return { hasError: true }; } componentDidCatch(error, info) { this.context.log(error, info); } componentDidUpdate(prevProps) { if (prevProps.children !== this.props.children) { this.setState({ hasError: false }); } } render() { if (this.state.hasError) { return this.props.fallback; } return this.props.children; } } _rollupPluginBabelHelpers.defineProperty(ErrorBoundary, "propTypes", { children: PropTypes__default["default"].node, fallback: PropTypes__default["default"].node }); _rollupPluginBabelHelpers.defineProperty(ErrorBoundary, "contextType", ErrorBoundaryContext.ErrorBoundaryContext); exports["default"] = ErrorBoundary;