Fragments

what is Fragments in React ?

  • Profile picture of Mcs
  • by Mcs July 3, 2025

A Fragment lets you group multiple elements without adding extra nodes to the DOM (like a <div>).

Why Use Fragments?

  1. Avoid unnecessary wrapper <div> tags
  2. Cleaner and semantic HTML

Example: With <div> (adds extra tag)

function App() {
 return (
   <div>
     <h1>Hello</h1>
     <p>This is a message.</p>
   </div>
 );
}

Example: With Fragment (no extra tag)

import React from 'react';

function App() {
 return (
   <React.Fragment>
     <h1>Hello</h1>
     <p>This is a message.</p>
   </React.Fragment>
 );
}

Shorthand Syntax

<>
 <h1>Hello</h1>
 <p>This is a message.</p>
</>

Output won't contain any wrapper div — just the <h1> and <p>.

Summary: Fragments

FeatureDescription
<></>Short syntax for fragments
PurposeReturn multiple elements without extra tags
OutputDoesn’t add nodes to the DOM

Comments

Add new comment

Restricted HTML

  • Allowed HTML tags: <br> <p> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <cite> <dl> <dt> <dd> <a hreflang href> <blockquote cite> <ul type> <ol type start> <strong> <em> <code> <li>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.