最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the Flow equivalent of React.PropTypes.node? - Stack Overflow

programmeradmin2浏览0评论

What is the Flow equivalent of React.PropTypes.node (i.e., anything that can be rendered by React, if there is one? Do I have to create it myself as a union type?

In other words, what would replace ??? here?

type Props = {
  children: ???,
}

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

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

What is the Flow equivalent of React.PropTypes.node (i.e., anything that can be rendered by React, if there is one? Do I have to create it myself as a union type?

In other words, what would replace ??? here?

type Props = {
  children: ???,
}

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

UselessComponent.propTypes = {
  children: React.PropTypes.node.isRequired,
}
Share Improve this question edited Jan 3, 2019 at 9:22 Gianfranco P 10.8k7 gold badges54 silver badges68 bronze badges asked Nov 28, 2016 at 15:41 dontexistdontexist 5,6526 gold badges29 silver badges53 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 10

It looks like it is still an issue here.

According to the discussion in that issue, what you should do until it is fixed:

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

For versions of flow >= 0.53, use the new type React.Node for props.children and anywhere you are expecting a renderable node.

The definition of React.Node can be roughly approximated with a React.ChildrenArray:

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

There was a major rework of the react types in flow 0.53. Summary of the changes and instructions on how to migrate are in the release notes. The flow docs explain how to use in detail.

For example:

import type { Node } from 'react';

type Props = {
  input?: Node,
}
发布评论

评论列表(0)

  1. 暂无评论