React

2019-02-20 16:09 更新

React 16之前的版本,僅需接入插件即可,無(wú)需額外配置。

對(duì)于React 16及其以后的版本,需要在src/index.js中進(jìn)行額外配置:

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

  componentDidCatch(error, info) {
    this.setState({ hasError: true });
    // 將component中的報(bào)錯(cuò)發(fā)送到Fundebug
    fundebug.notifyError(error, {
      metaData: {
        info: info
      }
    });
  }

  render() {
    if (this.state.hasError) {
      return null
      // 也可以在出錯(cuò)的component處展示出錯(cuò)信息
      // return <h1>出錯(cuò)了!</h1>;
    }
    return this.props.children;
  }
}

ReactDOM.render( < ErrorBoundary > < App / > < /ErrorBoundary>, document.getElementById('root'));

Demo項(xiàng)目:fundebug-react-demo


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)