【问题标题】:Flow is missing props validation when setting default values on an object在对象上设置默认值时,流程缺少道具验证
【发布时间】:2017-07-27 09:47:24
【问题描述】:

我有一个 React 函数式组件,它以 List 作为道具。

const Dashboard = (props: { items: List } = { items: List() }) => {
  const { items } = props
  ...
})

但是,const { items } = props 中的 items 会产生以下错误:

error 'items' is missing in props validation react/prop-types

FlowType docs举个例子:

ES2015 特性 分配给参数的默认值必须在后面 参数的类型注解:

函数 foo (P1: T1 = V): U { .. }

但我不太清楚这如何应用于对象

【问题讨论】:

    标签: javascript eslint flowtype static-typing


    【解决方案1】:

    这实际上不是流错误而是这个 eslint 错误:

    https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prop-types.md

    如果您现在使用 flow 时不关心 proptype,则可以通过禁用 eslint 规则来解决您的错误。

    或者你添加一个proptype,它不像flow那么具体,而是runtime。

    Dashboard.propTypes = {
      items: React.PropTypes.object
    };
    

    【讨论】:

    • 酷!非常感谢您指出这一点。我会抑制错误。
    猜你喜欢
    • 2021-09-04
    • 2017-08-17
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 2015-11-18
    • 2018-08-01
    • 2019-03-18
    相关资源
    最近更新 更多