reactjs - eslint `forbid-prop-types` 规则导致 Firefox 控制台窗口出现警告

标签 reactjs eslint

正如文件所述here

array and object can be replaced with arrayOf and shape, respectively.

所以,我在我的项目中这样做了:

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
}

问题:

如何避免 firefox 中出现警告消息,并通过 eslint 语法检查(最好不要修改 eslint 默认规则)?

顺便说一句:firefox 警告如下

Warning: Failed prop type: X: prop type classes is invalid; it must be a function, usually from the prop-types package, but received undefined.

最佳答案

您需要定义 Prop 的形状。如果您不确定,只需 console.log() Prop 并查看其组成。 Click here to view the available PropType declarations .

例如,如果您要使用 redux 存储作为 react-router- 中的 this.props.storehistory dom 作为 this.props.history:

enter image description here

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

使用isRequired时要小心,因为有时函数/数组/对象/值/等可能存在也可能不存在。对于上面的示例,所有这些都应该在组件安装时出现。

关于reactjs - eslint `forbid-prop-types` 规则导致 Firefox 控制台窗口出现警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53255824/

相关文章:

javascript - react 网格布局

reactjs - React Router v4 渲染组件两次

javascript - Facebook Flow - 如何从深层嵌套对象返回类型转换属性?

linux - 得到 'Configuration for rule "no-unused-expressions"is invalid' on linux but not on osx

javascript - 格式化第三方导入和其他导入之间的空行

reactjs - 偶尔会出现在 useEffect() 中创建清理函数的警告

html - 为什么移动设备上的 Safari 需要很长时间才能从内存中加载图像?

javascript - 对象中的 ES6 风格简写函数是否有 ESLint 规则?

javascript - 为 TSLint (NestJs) 和 ESLint (VueJs) 设置 VS Code linting

reactjs - “Proptypes”未定义