如何在React中创建带有开始和结束标记的DOM组件?
在这里,我创建了一个组件 SomeComponent
const SomeComponent = () => (
<div className={"some-class"}>
some text here
</div>
)
我可以像这样使用这个组件:
<SomeComponent text={"here is some text"} />
我想做同样的事情,但有一个开始和结束标签,如下所示:
<SomeComponent>
here is some text
</SomeComponent>
我怎样才能做到这一点?
非常感谢 - 奥利
回答
您可以访问“内容”作为children属性。
const SomeComponent = ({ children }) => (
<div className={"some-class"}>
{children}
</div>
)