Why are Fragments in React 16 better than container divs?

后端 未结 6 1715
陌清茗
陌清茗 2020-12-22 15:39

In React 16.2, improved support for Fragments has been added. More information can be found on React\'s blog post here.

We are all familiar with the fol

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-22 16:28

    1. It’s a tiny bit faster and has less memory usage (no need to create an extra DOM node). This only has a real benefit on very large and/or deep trees, but application performance often suffers from death by a thousand cuts. This is one cut less.
    2. Some CSS mechanisms like Flexbox and CSS Grid have a special parent-child relationship, and adding divs in the middle makes it hard to keep the desired layout while extracting logical components.
    3. The DOM inspector is less cluttered. :-)

    You can find the descriptions of some other use cases in this React issue: Add fragment API to allow returning multiple components from render

提交回复
热议问题