在React中获取子属性

kha*_*iuk 2 javascript reactjs

我有两个组成部分:CellScreenCell旨在成为屏幕的孩子,我将像这样使用它们:

<Screen>
  <Cell x={1} y={2}/>
  <Cell x={1} y={1}/>
</Screen>
Run Code Online (Sandbox Code Playgroud)

问题:我怎样才能xy道具里面Screenrender方法?像这样:

render() {
  var {children} = this.props
  children[0].x // it doesn't work
...
Run Code Online (Sandbox Code Playgroud)

小智 6

props在大多数情况下,您需要通过访问它们。像: children[0].props.x