How do I iterate through a React component's children in Typescript tsx?
The following would be valid jsx:
public render() {
return (
<div>
{React.Children.map(this.props.children, x => x.props.foo)}
</div>
);
}
However, in Typescript, the type of x is React.ReactElement<any>
so I have no access to props. Is there some kind of casting that I need to do?
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?
Yes. Also prefer the term assertion
. A quick one:
React.Children.map(this.props.children, x => (x as any).props.foo)