2015-12-01 24 views
5

Come faccio a scorrere i figli di un componente React in Typescript tsx?Come ripetere i bambini di un componente in Riscontro al dattiloscritto?

Di seguito sarebbe JSX valida:

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

Tuttavia, a macchina, il tipo di x è React.ReactElement<any> quindi non ho accesso a oggetti di scena. C'è qualche tipo di casting che devo fare?

risposta

11

Tuttavia, in Typescript, il tipo di x è React.ReactElement quindi non ho accesso agli oggetti di scena. C'è qualche tipo di casting che devo fare?

Sì. Preferisci anche il termine assertion. Uno veloce:

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