gpt4 book ai didi

reactjs - 如何获取更有用的错误消息?

转载 作者:行者123 更新时间:2023-12-03 14:28:50 25 4
gpt4 key购买 nike

每当我尝试访问 undefined 时,React + ES6 Babel 都会发出以下错误消息(例如尝试 person.age 其中 person === undefined.)

enter image description here

当我输错 import 语句或在解构 props 对象时出错时,也会发生这种情况。例如,我可能会犯以下错误:

const { name, age } = this.props.person
// but `this.props.person` only has a `name` property.

缺少错误消息是一件痛苦的事情。是否有一些我错过设置的选项?大家都是怎么解决这个问题的呢?

最佳答案

这是一个强烈要求的功能,可能会在下一个 React 版本中实现。现在您可以使用redbox-react 。据我所知react-transform-catch-errors已弃用。

/* global __DEV__ */
import React from 'react'
import { render } from 'react-dom'
import App from './components/App'

const root = document.getElementById('root')

if (__DEV__) {
const RedBox = require('redbox-react').default
try {
render(<App />, root)
} catch (e) {
render(<RedBox error={e} />, root)
}
} else {
render(<App />, root)
}

关于reactjs - 如何获取更有用的错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38290686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com