Umi中 的 React 异常捕获组件在失效

我的基础组件

export default class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }
export default class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  componentDidCatch(error, errorInfo) {
    this.setState({ hasError: true });
  }
  onError = () => {
    throw new Error('出现了错误');
  };
  render() {
    if (this.state.hasError) {
      return <h1>页面错误的安全页面</h1>;
    }

    return (
      <div>
        <h1
          onClick={() => {
            this.onError();
          }}
        >
          点我报错
        </h1>
        <div>{this.props.children}</div>
      </div>
    );
  }
}

  componentDidCatch(error, errorInfo) {
    this.setState({ hasError: true });
  }
  onError = () => {
    throw new Error('出现了错误');
  };
  render() {
    if (this.state.hasError) {
      return <h1>Something went wrong.</h1>;
    }

    return (
      <div>
        <h1
          onClick={() => {
            this.onError();
          }}
        >
          点我报错
        </h1>
        <div>{this.props.children}</div>
      </div>
    );
  }
}

然后点击报错按钮制造一个错误,然后componentDidCatch钩子是捕捉到了信息,到时界面还是崩溃了,该怎么解决,使得页面出现异常崩溃时,显示 '页面错误的安全页面'

image.png

阅读 1.4k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题