UNPKG

linkmore-design

Version:

🌈 🚀lm组件库。🚀

44 lines 1.41 kB
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _inherits from "@babel/runtime/helpers/esm/inherits"; import _createSuper from "@babel/runtime/helpers/esm/createSuper"; import React from 'react'; var ErrorBoundary = /*#__PURE__*/function (_React$Component) { _inherits(ErrorBoundary, _React$Component); var _super = _createSuper(ErrorBoundary); function ErrorBoundary(props) { var _this; _classCallCheck(this, ErrorBoundary); _this = _super.call(this, props); _this.state = { hasError: false }; return _this; } _createClass(ErrorBoundary, [{ key: "componentDidCatch", value: function componentDidCatch(error, errorInfo) { // 你同样可以将错误日志上报给服务器 // logErrorToMyService(error, errorInfo); } }, { key: "render", value: function render() { if (this.state.hasError) { // 你可以自定义降级后的 UI 并渲染 return this.props.fallback; } return this.props.children; } }], [{ key: "getDerivedStateFromError", value: function getDerivedStateFromError(error) { // 更新 state 使下一次渲染能够显示降级后的 UI return { hasError: true }; } }]); return ErrorBoundary; }(React.Component); export default ErrorBoundary;