UNPKG

@carbon/react

Version:

React components for the Carbon Design System

47 lines (45 loc) 1.45 kB
/** * Copyright IBM Corp. 2016, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ const require_runtime = require("../../_virtual/_rolldown/runtime.js"); const require_ErrorBoundaryContext = require("./ErrorBoundaryContext.js"); let react = require("react"); react = require_runtime.__toESM(react); let prop_types = require("prop-types"); prop_types = require_runtime.__toESM(prop_types); //#region src/components/ErrorBoundary/ErrorBoundary.tsx /** * Copyright IBM Corp. 2016, 2023 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ var ErrorBoundary = class extends react.default.Component { constructor(..._args) { super(..._args); 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; } }; ErrorBoundary.propTypes = { children: prop_types.default.node, fallback: prop_types.default.node }; ErrorBoundary.contextType = require_ErrorBoundaryContext.ErrorBoundaryContext; //#endregion exports.default = ErrorBoundary;