【发布时间】:2017-08-22 23:51:21
【问题描述】:
如果我将父组件的子组件键入为Node,那么它表示children 是传递给React.Children.map 的错误类型。但是,如果我将其键入为ChildrenArray<any>,那么它表示我传递给React.cloneElement 的类型不正确。
import * as React from 'react';
type Props = {
children?: React.ChildrenArray<any>,
};
class Test extends React.Component<Props> {
props: Props;
render() {
return React.Children.map(this.props.children, child =>
React.cloneElement(child, {})
);
}
}
<Test>
<span>foo</span>
<span>bar</span>
</Test>
import * as React from 'react';
type Props = {
children?: Node,
};
class Test extends React.Component<Props> {
props: Props;
render() {
return React.Children.map(this.props.children, child =>
React.cloneElement(child, {})
);
}
}
<Test>
<span>foo</span>
<span>bar</span>
</Test>
【问题讨论】:
-
您的第一个示例错误仅包含 flow.org/try/…,但通过 flow.org/try/… 传递,所以肯定看起来很奇怪。