如何使用React创建React元素.createElement with child?



我想用React Component with child创建元素。

我的React组件:

const Element = () => {
return (
<div class='element'>/* child should be here*/</div>
);
}
export default Element;

我想这样做:

React.createElement(Element, {}, [
React.createElement('span', { className: 'text' }, 'text 1'),
React.createElement('span', { className: 'text' }, 'text 2')
])

输出应该是:

<div class="element">
<span class="text">text 1</span>
<span class="text">text 2</span>
</div>

还是我做错了?

有一个名为children的道具,它包含了组件中的子组件

所以你可以使用

const Element = ({ children }) => {
return (
<div className='element'>{children}</div>
);
}
export default Element;

和html属性class应该是className在反应