REACT MASTER CLASS /// CLEAN DOM /// NO DIV SOUP /// REACT FRAGMENTS /// REACT MASTER CLASS /// CLEAN DOM /// NO DIV SOUP /// REACT FRAGMENTS ///

React Fragments

Group multiple elements without wrapping them in unnecessary divs. Keep your layout clean and your DOM performant.

Component.jsx
1 / 13
12345
🧩

Tutor:In React, a component can only return ONE single root element. This is because a React component essentially compiles down to a JavaScript function returning an object.


Skill Matrix

UNLOCK NODES BY LEARNING FRAGMENT PATTERNS.

Concept: Fragments

Fragments let you group children without adding extra DOM nodes. Avoid div soup!

System Check

What is the short syntax for a React Fragment?


Community Holo-Net

Showcase Your Clean DOM

ACTIVE

Share your component refactors removing div-soup with the rest of the developers.

React Fragments & Clean DOMs

Author

Pascual Vila

Frontend Instructor // Code Syllabus

A common pattern in React is for a component to return multiple elements. Since JavaScript functions cannot return multiple values independently, React requires you to wrap them. Enter React Fragments.

The Problem: Div Soup

Often, developers wrap adjacent JSX elements in a <div>. While this solves the React compiler error, it litters your actual HTML DOM with unnecessary wrappers. We call this "Div Soup." Overusing divs can accidentally break carefully crafted CSS Grid or Flexbox layouts by inserting unexpected parent containers between the flex container and its intended flex children.

React.Fragment

<React.Fragment> is a built-in React component that lets you group a list of children without adding an extra node to the DOM. When React renders the component, the Fragment disappears, leaving only your inner elements in the HTML output.

Short Syntax

Typing <React.Fragment> over and over gets tedious. React offers an elegant short syntax using empty tags: <>...</>. In 99% of cases, you should use the short syntax to keep your JSX clean and readable.

Keys and Loops

The only time you cannot use the short syntax is when you are mapping over an array and need to pass a key prop. The short syntax does not support attributes or props. If you need a key, you must use the explicit <React.Fragment key={id}> syntax.

View Full Transcript+

This section contains the full detailed transcript covering why JSX requires a single parent element (because it transpiles to React.createElement, which returns a single object), the performance impact of Deep DOM trees, and alternative ways to return arrays of elements from components prior to React 16.

React Glossary

React Fragment

A React feature that allows you to group multiple elements without adding an extra node to the real DOM.

snippet.jsx

Short Syntax

The shorthand notation for React.Fragment, represented by empty tags. Cannot accept props like 'key'.

snippet.jsx

Div Soup

An anti-pattern where developers unnecessarily nest elements inside <div> tags merely to satisfy JSX syntax rules.

snippet.jsx

Key Prop

A special string attribute you need to include when creating lists of elements. Essential when mapping Fragments.

snippet.jsx

DOM Node

An actual object in the browser's Document Object Model. Fragments prevent the creation of unnecessary DOM Nodes.

snippet.jsx

JSX

A syntax extension for JavaScript. JSX requires components to return a single parent element.

snippet.jsx