gpt4 book ai didi

reactjs - 对于 PropTypes 中描述的未定义的 props,使 React 产生错误而不是警告

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

我的组件有以下定义:

import React, { PureComponent } from 'react'
import PropTypes from 'prop-types';

class MyElement extends PureComponent {

static propTypes = {
myField: PropTypes.object.isRequired,
//...
};


render() {...}
}

export default MyElement

问题是我最终在控制台中收到警告,说

Warning: Failed prop type: The prop onChange is marked as required in SimpleSelect, but its value is undefined.

相反,我想让React在运行npm start命令时产生错误消息。是否可以 ?或者我必须使用完全不同的方法来实现这一目标?

最佳答案

myfield:Proptypes.objectOf (function(propValue, key, componentName, location, propFullName) {
if (!/matchme/.test(propValue[key])) {
return new Error(
'Invalid prop `' + propFullName + '` supplied to' +
' `' + componentName + '`. Validation failed.'
);
}
})

您可以使用这样的验证器函数来显示错误。请查看reactjs文档。

关于reactjs - 对于 PropTypes 中描述的未定义的 props,使 React 产生错误而不是警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55007035/

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