Accessing React component children props from the parent

前端 未结 4 1286
野的像风
野的像风 2021-01-07 16:36

I am currently in the process of designing the interface for a game engine that is written in JavaScript and ReactJS.

If a game object has a texture, the source of t

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-07 17:17

    Because the links in the comments are broken, I enclose a link to the current react-router Switch module implementation where children are parsed (see the render() function). Also in this tutorial video the author accesses children props from parent.

    To not bump into the same problem with link expiring again, here is how the code for accessing children props from parent would look like for above example when inspired by react-router's Switch:

    (inside GameObject)

    const { children } = this.props
    
    React.Children.forEach(children, element => {
      if (!React.isValidElement(element)) return
    
      const { source } = element.props
    
      //do something with source..
    })
    

提交回复
热议问题