如何动态地将子组件注入父组件?例如打开这个:
<Parent>
<Child/>
<Child/>
</Parent>
到这个:
<Parent>
<Child/>
<Child/>
<Child/>
</Parent>
不使用通常的解决方案,这显然是map
。事实是,有一个库可以通过计算props.children
组件的属性来使API工作,因此该map
功能在这里实际上并不起作用。
使用React的Children
助手
const Component = ({children}) =>{
return React.Children.map(children, child =>{
return React.cloneElement(child, {childProps : 'foo'})
})
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句