相关疑难解决方法(0)

使用ErrorBoundary创建React App不显示错误消息

我正在学习如何使用componentDidCatch().它看起来很直接.它可以工作,但仍然在视图上显示完整的错误堆栈.

在单独的文件中:

import React, { Component } from 'react'

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

  componentDidCatch(error, info) {
    console.log("Catching an error") // this is never logged
    this.setState(state => ({...state, hasError: true }))
  }

  render() {
    if (this.state.hasError) { return <div>Sorry, an error occurred</div> }

    return this.props.children
  }
}
export default ErrorBoundary

      ...

import React, { Component } from 'react'

class Foo extends Component {

  render() {
    return this.props.a.b; // …
Run Code Online (Sandbox Code Playgroud)

reactjs create-react-app

11
推荐指数
2
解决办法
2246
查看次数

标签 统计

create-react-app ×1

reactjs ×1