在React 16.2中,增加了对的支持Fragments。可以在React的博客文章中找到更多信息。
Fragments
我们都熟悉以下代码:
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首选?它对性能有帮助吗?如果是这样,为什么?希望有一些见识。
Fragment
div
您可以在此React问题中找到一些其他用例的描述:添加片段API以允许从render返回多个组件