如何在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)