如何渲染在 React 中作为子项传递的 JSX?

How to render the JSX that is passed as a child in React?

有没有办法让 React 组件拥有动态内容?沿着这些路线,

function Div(){return(<div style={{backgroundColor:"black"}}></div>)

然后像这样在 App.js 中使用它

<Div><p>ex</p></Div>

谢谢!

是的,只需使用 children 道具:

function Div({ children }){ 
 return (<div style={{backgroundColor:"black"}}>{children}</div>)
}

这里有一个 codesandbox 演示不同的方法