react异常处理

react16是有个异常处理生命周期----componentDidCatch

单个组件的处理方法:
class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; } componentDidCatch(error, info) { this.setState({ hasError: true }); logErrorToMyService(error, info); } render() { if (this.state.hasError) { return <h1>界面报错了。。。。</h1>; } return this.props.children; } }

 可以把这个异常扑捉方法放在路由里面。

posted @ 2020-11-12 17:30  薄荷+猫  阅读(443)  评论(0编辑  收藏  举报