Free ATS Friendly Resume Builder Online

Create Your Resume

Resume Builder

Resume Maker

Resume Templates

Resume PDF Download

Create Your Resume is a free online resume builder that helps job seekers create professional, ATS friendly resumes in minutes. Easily build, customize, and download modern resume templates in PDF format.

Our resume maker is designed for freshers and experienced professionals looking to create job-ready resumes. Choose from multiple resume templates, customize sections, and generate ATS optimized resumes online for free.

Create resumes for IT jobs, software developers, freshers, experienced professionals, managers, and students. This free resume builder supports CV creation, resume PDF download, and online resume editing without signup.

Back to React Js
Lesson 18 of 59

What Are Fragments in React and How to Use Them

Fragments in React are used to group multiple elements without adding extra nodes to the DOM. They allow components to return multiple elements while keeping the rendered HTML clean and minimal. React Fragments are especially useful when building lists, tables, or layouts where unnecessary wrapper elements can break styling or structure. By using fragments, developers can write cleaner JSX, improve performance, and maintain semantic HTML. Understanding React Fragments is important for writing professional, scalable React components without cluttering the DOM.

In React, every component must return a single parent element. This rule exists because JSX expressions must evaluate to one root element. However, in many real-world scenarios, adding an extra wrapper element like a div is unnecessary and can cause problems with styling, layout, or semantics. React Fragments solve this problem.

A React Fragment allows you to group multiple elements together without introducing an extra HTML element into the DOM. This keeps the DOM structure clean while still satisfying React’s requirement of a single return element.

Problem Without Fragments

Consider a component that needs to return two sibling elements:


function Message() {
  return (
    <h1>Hello</h1>
    <p>Welcome to React</p>
  );
}

This code will throw an error because JSX requires a single parent element.

A common workaround is to wrap elements in a div:


function Message() {
  return (
    <div>
      <h1>Hello</h1>
      <p>Welcome to React</p>
    </div>
  );
}

While this works, it adds an extra div to the DOM, which may not always be desirable.

Using React Fragments

React Fragments allow you to group elements without adding an extra DOM node.

Example using Fragment syntax:


import React from "react";

function Message() {
  return (
    <React.Fragment>
      <h1>Hello</h1>
      <p>Welcome to React</p>
    </React.Fragment>
  );
}

This renders both elements without adding an extra wrapper element in the DOM.

Short Syntax for Fragments

React also provides a shorthand syntax for fragments, which is more commonly used:


function Message() {
  return (
    <>
      <h1>Hello</h1>
      <p>Welcome to React</p>
    </>
  );
}

This shorthand behaves exactly like React.Fragment but is cleaner and easier to read.

Fragments with Lists and Keys

Fragments are especially useful when rendering lists where multiple elements are returned for each item.

Example:


function List() {
  const items = ["A", "B", "C"];

  return (
    <ul>
      {items.map(item => (
        <>
          <li>{item}</li>
          <li>Details</li>
        </>
      ))}
    </ul>
  );
}

However, when using fragments inside a list, a key is required. The shorthand syntax does not support keys, so the full Fragment syntax must be used.

Correct example with key:


{items.map(item => (
  <React.Fragment key={item}>
    <li>{item}</li>
    <li>Details</li>
  </React.Fragment>
))}

Real-World Scenario

Fragments are commonly used in table layouts. HTML tables require specific structures like trtd, and th. Adding extra wrapper elements can break table rendering. Fragments allow developers to return multiple table cells without inserting invalid HTML.

Example:


function TableRow() {
  return (
    <>
      <td>John</td>
      <td>Developer</td>
    </>
  );
}

This component can be safely used inside a tr element.

Important Notes and Best Practices

Fragments do not support attributes like className or style. If you need to apply styling or attributes, a real DOM element must be used instead. Use fragments when grouping elements purely for structure, not for styling or layout.

Avoid unnecessary wrapper elements whenever possible to keep the DOM clean and improve readability.

In summary, React Fragments are a powerful feature that allows components to return multiple elements without adding extra DOM nodes. They help maintain clean HTML, prevent layout issues, and make React components more efficient and readable.