在 React 16.2 中,Fragments
添加了改进的支持。更多信息可以在 React 的博客文章中找到。
下面的代码我们都很熟悉:
render() {
return (
// Extraneous div element :(
<div>
Some text.
<h2>A heading</h2>
More text.
<h2>Another heading</h2>
Even more text.
</div>
);
}
是的,我们需要一个容器 div,但这没什么大不了的。
在 React 16.2 中,我们可以这样做以避免周围的容器 div:
render() {
return (
<Fragment>
Some text.
<h2>A heading</h2>
More text.
<h2>Another heading</h2>
Even more text.
</Fragment>
);
}
在任何一种情况下,我们仍然需要一个容器元素包围内部元素。
我的问题是,为什么使用Fragment
首选?它对性能有帮助吗?如果是这样,为什么?会喜欢一些见解。