Reactjs 对PropTypes中描述的未定义的道具进行React屈服错误而不是警告
我对我的组件有以下定义:Reactjs 对PropTypes中描述的未定义的道具进行React屈服错误而不是警告,reactjs,Reactjs,我对我的组件有以下定义: import React, { PureComponent } from 'react' import PropTypes from 'prop-types'; class MyElement extends PureComponent { static propTypes = { myField: PropTypes.object.isRequired, //... }; render() {...} }
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types';
class MyElement extends PureComponent {
static propTypes = {
myField: PropTypes.object.isRequired,
//...
};
render() {...}
}
export default MyElement
问题是,我最终在控制台中发出警告,说
警告:失败的道具类型:道具onChange
标记为需要
在SimpleSelect中
,但其值为未定义
相反,我希望在运行npmstart
命令时生成错误消息。可能吗?或者我必须使用完全不同的方法来实现这一点
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文档。我只知道如何在测试中执行此操作。您可以使用jest prop type error。我希望这会有帮助。我相信道具类型是一个运行时类型检查器。如果您想在构建期间看到类型错误,可能需要查看Flow或TypeScript。