【问题标题】:What is the Flow equivalent of React.PropTypes.node?React.PropTypes.node 的 Flow 等价物是什么?
【发布时间】:2017-04-12 09:42:31
【问题描述】:

什么是 React.PropTypes.node 的 Flow 等价物(即,任何可以由 React 呈现的东西,如果有的话?我必须自己创建它作为联合类型吗?

换句话说,这里的??? 会用什么代替?

type Props = {
  children: ???,
}

const UselessComponent
  : (props: Props) => React$Element<*>
  = ({ children }) => (
    <div>
      {children}
    </div>
  )

UselessComponent.propTypes = {
  children: React.PropTypes.node.isRequired,
}

【问题讨论】:

    标签: javascript reactjs types flowtype


    【解决方案1】:

    看起来这仍然是一个问题here

    根据该问题中的讨论,在修复之前您应该做什么:

    type Props = {
      children?: React.Element<*>,
    };
    

    【讨论】:

      【解决方案2】:

      对于 flow >= 0.53 的版本,使用新类型 React.Node 表示 props.children 以及您期望可渲染节点的任何位置。

      React.Node 的定义可以大致近似为 React.ChildrenArray:

      type Node = React.ChildrenArray<void | null | boolean | string |
      number | React.Element<any>>;
      

      在流程 0.53 中对反应类型进行了重大修改。 release notes 中提供了有关如何迁移的更改和说明的摘要。 flow docs详细讲解如何使用。

      例如:

      import type { Node } from 'react';
      
      type Props = {
        input?: Node,
      }
      

      【讨论】:

        猜你喜欢
        • 2020-05-24
        • 2017-10-21
        • 2014-05-08
        • 2014-06-12
        • 1970-01-01
        • 2022-11-28
        • 1970-01-01
        • 2018-07-10
        • 2023-04-10
        相关资源
        最近更新 更多