@carbon/react
Version:
React components for the Carbon Design System
44 lines (42 loc) • 1.22 kB
JavaScript
/**
* 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.
*/
import { ErrorBoundaryContext } from "./ErrorBoundaryContext.js";
import React from "react";
import PropTypes from "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.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: PropTypes.node,
fallback: PropTypes.node
};
ErrorBoundary.contextType = ErrorBoundaryContext;
//#endregion
export { ErrorBoundary as default };