Reactjs 反应容器道具儿童通过

Reactjs 反应容器道具儿童通过,reactjs,Reactjs,恐怕那个问题很简单很愚蠢,但我被卡住了( 我有一个包含一些嵌套组件的长方体组件: <Box.Content anyProp="prop"> <Box.Text... <Box.Links 可以使用动态设置父对象对其子对象的道具 运行以下代码段。Parent组件上的prop1和prop2将传递给Child1和Child2组件 constchild1=(props)=>Child1:{JSON.stringify(props)}; const

恐怕那个问题很简单很愚蠢,但我被卡住了(

我有一个包含一些嵌套组件的长方体组件:

<Box.Content anyProp="prop">
  <Box.Text...
  <Box.Links

可以使用动态设置父对象对其子对象的道具

运行以下代码段。
Parent
组件上的
prop1
prop2
将传递给
Child1
Child2
组件

constchild1=(props)=>Child1:{JSON.stringify(props)}

; constchild2=(props)=>子2:{JSON.stringify(props)}

; 常量父项=({children,…props})=>{ children=children.map(v=>React.cloneElement(v,props)); 返回{children}; }; 常量应用=()=>( ); const rootElement=document.getElementById(“根”); ReactDOM.render(,rootElement);


为了澄清,在您的示例中,您需要
anyProp=“prop”
应用于
框。文本
框。链接
?是的,如果可以的话,它是正确的,当我写谢谢你!如果我不需要所有道具,但需要一个道具名?可能是它更简单的解决方案?要指定某个道具,你只需要使用
React.cloneElement(v,props.PropName)
BoxComponent.Content = (...props) => (
  <BoxContent {...props}>{props.children}</BoxContent>
);