reactjs – 如何在Typescript React中遍历Component的Children?

前端之家收集整理的这篇文章主要介绍了reactjs – 如何在Typescript React中遍历Component的Children?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在Typescript tsx中遍历React组件的子项?

以下是有效的jsx:

public render() {
    return (
        <div>
            {React.Children.map(this.props.children,x => x.props.foo)}
        </div>
    );
}

但是,在Typescript中,x的类型是React.ReactElement< any>所以我无法获得道具.我需要做某种铸造吗?

However,in Typescript,the type of x is React.ReactElement so I have no access to props. Is there some kind of casting that I need to do?

是.也更喜欢断言一词.一个快速的:

React.Children.map(this.props.children,x => (x as any).props.foo)

猜你在找的React相关文章