InterviewHubPRO
Section Overview
Q32

Question 32 of 445

Part 1: Core React, JSX & Component Architecture (1 - 77)

7%
React & Modern Frontend (445 Qs)•Part 1: Core React, JSX & Component Architecture (1 - 77)

Why fragments are better than container divs?

Below are the list of reasons to prefer fragments over container DOM elements,

  1. Fragments are a bit faster and use less memory by not creating an extra DOM node. This only has a real benefit on very large and deep trees.
  2. Some CSS mechanisms like Flexbox and CSS Grid have a special parent-child relationships, and adding divs in the middle makes it hard to keep the desired layout.
  3. The DOM Inspector is less cluttered.

Practical Code Example

jsx
// Fragment vs Div: Avoids invalid HTML tables and unnecessary CSS Flex/Grid wrapper bugs
function TableColumns() {
  return (
    <>
      <td>First Column</td>
      <td>Second Column</td>
    </>
  );
}