简体   繁体   English

eslint `forbid-prop-types` 规则在 Firefox 控制台窗口中导致警告

[英]eslint `forbid-prop-types` rule cause a warning in firefox console window

As the document says here正如文件所说的here

array and object can be replaced with arrayOf and shape , respectively. array 和object可以分别替换为 arrayOf 和shape

So, I did this in my project:所以,我在我的项目中这样做了:

class X extends React.Component {
....
}

X.propTypes = {
    somePorp: PropTypes.shape.isRequired, // this cause an warnning in firefox, but satisfied eslint syntax check
    somePorp: PropTypes.object.isRequired, // no warnning in firefox, but can not satisfy eslint syntax check
}

Question :问题

How can I avoid warning message in firefox, as well as pass eslint syntax check (it's better no modification of eslint default rule)?如何避免在 Firefox 中出现警告消息,以及通过 eslint 语法检查(最好不要修改 eslint 默认规则)?

BTW: firefox warning is something like below顺便说一句:firefox 警告如下所示

Warning: Failed prop type: X: prop type classes is invalid;警告:失败的道具类型:X:道具类型classes无效; it must be a function, usually from the prop-types package, but received undefined .它必须是一个函数,通常来自prop-types包,但收到undefined

You need to define the shape of the prop.您需要定义道具的形状 If you're unsure, simply console.log() the prop and view it's make up.如果您不确定,只需console.log()道具并查看它的组成。 Click here to view the available PropType declarations .单击此处查看可用的 PropType 声明

For example, if you were to utilize redux store as this.props.store or history from react-router-dom as this.props.history :例如,如果您要将redux存储用作this.props.store或将react-router-dom history用作this.props.history

在此处输入图片说明

history: PropTypes.objectOf(PropTypes.func).isRequired,       // object of funcs
store: PropTypes.shape({                                      // shape of...
  dispatch: PropTypes.func.isRequired,                          // func
  getState: PropTypes.func.isRequired,                          // func
  liftedStore: PropTypes.objectOf(PropTypes.func).isRequired,   // object of funcs
  replaceReducer: PropTypes.func.isRequired,                    // func
  subscribe: PropTypes.func.isRequired,                         // func
  Symbol: PropTypes.func.isRequired,                            // func
}).isRequired

Be careful when using isRequired , because sometimes the function/array/object/value/etc may or may not exist.使用isRequired时要小心,因为有时函数/数组/对象/值/等可能存在也可能不存在。 For the example above, all of them should be present upon component mount.对于上面的示例,所有这些都应该在组件安装时出现。

You can also use the following way:您还可以使用以下方式:

X.propTypes = {
    somePorp:  PropTypes.objectOf(PropTypes.object()),  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM