简体   繁体   English

儿童反应元素的流式注释

[英]flow type annotation for children react elements

Is there a better way to type-annotate children ? 是否有更好的方法来为children打字?

type FilterLinkProps={
  filter:State$VisibilityFilter,
  children:any
};

const FilterLink = ({
  filter,
  children
}:FilterLinkProps) => {
  return (
    <a href='#'
      onClick={e => {
        e.preventDefault();
        store.dispatch(({
          type: 'SET_VISIBILITY_FILTER',
          filter
        }:Action$VisibilityFilter));
      }}
    >
    {children}
    </a>
  );
};

Flow v0.53 supports children out of the box!! Flow v0.53支持开箱即用的children

import * as React from 'react';

type Props = {
  children?: React.Node,
};

More info read official docs or the following blog post about it. 更多信息阅读官方文档或以下关于它的博客文章

For older versions of flow you can do the following: 对于旧版本的流,您可以执行以下操作:

import React from 'react';
import type { Children } from 'react';
type Props = {
  children?: Children,
};
const SampleText = (props: Props) => (
  <div>{props.children}</div>
);

Either case children should be declared as a nullable type. 无论哪种情况下, children应被宣布为可空类型。

It looks like that they will move some pieces forward with the arrival of fiber, hope they do! 看起来随着光纤的到来,他们会向前移动一些碎片,希望他们这样做!

Following the discussion in github github讨论之后

Cheat sheet: http://www.saltycrane.com/blog/2016/06/flow-type-cheat-sheet/ 备忘单: http //www.saltycrane.com/blog/2016/06/flow-type-cheat-sheet/

It doesn't look like. 它看起来不像。

From the official React libdef : 来自官方的React libdef

declare module react {
  declare var Children: any;
  ...
}

and then 接着

declare function cloneElement<Config> (
    element: React$Element<Config>,
    attributes: $Shape<Config>,
    children?: any
  ): React$Element<Config>;
type Text = string | number;
type Fragment = Iterable<Node>;
type ReactNode = React$Element<any>
  | Text
  | Fragment;

ReactNode should be the type of children, but caveat emptor. ReactNode应该是孩子的类型,但需要注意。

Source: I've seen this construction in some github issues on the flow repo, I believe. 资料来源:我相信,我已经在流动回购的一些github问题上看到过这种结构。

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

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