React
Fragments
Web Development
JavaScript
Frontend Development
UI Components
Performance Optimization

What are fragments in React, and why would you use them?

Listen to article
Deepak Tewatia
September 19, 2025
3 min read

Introduction

In React, fragments are special tools that let you group a list of items without adding extra elements to the page. This means you can return multiple elements from a component, and they won’t create unnecessary wrappers in the HTML. The main goal of using fragments is to keep the HTML clean and simple, which makes your code easier to manage.

Why Use Fragments?

Let’s break it down. When building a user interface, you often need to return more than one element from a component. Without fragments, the common way to do this is by wrapping those elements in a parent tag, like a <div>. This can lead to clutter in your HTML, which is not ideal. Using fragments helps you avoid that mess.

Here are a few reasons why you might want to use fragments:

  • Simpler HTML Structure: Fragments do not create additional elements in the DOM. This keeps the structure clean.
  • Better Performance: By reducing the number of DOM nodes, your app can perform a bit better, especially with large lists.
  • Easier Maintenance: Cleaner code is easier to read and maintain. You can quickly find and change what you need.
  • Conditional Rendering: Fragments come in handy when you need to render items conditionally without adding extra wrappers.

How to Use Fragments

Here’s the thing: it's incredibly simple to use fragments in your React code. There are two main ways to create a fragment. You can use the shorthand syntax or the full syntax. Let’s look at both.

1. Using Shorthand Syntax

The easiest way to use fragments is with the shorthand syntax. You can write it like this:

return (
    <>
        <h2>Title</h2>
        <p>This is a paragraph.</p>
    </>
);

2. Using Full Syntax

If you prefer or need to use the full syntax, you can do it like this:

return (
    <React.Fragment>
        <h2>Title</h2>
        <p>This is a paragraph.</p>
    </React.Fragment>
);

There is no difference in functionality between the two methods. You can choose whichever one suits your style or project needs best.

When to Use Fragments

What this really means is that fragments are useful in various situations. Here are a few scenarios where fragments shine:

  • Returning Multiple Elements: When your component needs to return multiple elements, and you don’t want a wrapping element.
  • Lists: When rendering lists where each item is a component. You can wrap them in a fragment instead of a <div>.
  • Conditional Rendering: If you are rendering elements based on certain conditions without needing an extra wrapper.

Example of Fragments in Action

Let’s look at a practical example where fragments can be beneficial. Imagine you are building a simple component that displays a list of items:

const ItemList = () => {
    return (
        <>
            <h2>Items</h2>
            <ul>
                <li>Item 1</li>
                <li>Item 2</li>
                <li>Item 3</li>
            </ul>
        </>
    );
};

In this example, using a fragment allows you to avoid wrapping everything in a <div>. That keeps your HTML cleaner.

Conclusion

In summary, fragments are a simple and powerful feature in React. They help you group items without cluttering your HTML, improving both performance and maintainability. Whether you use the shorthand or the full syntax, knowing how to use fragments will make your React development easier and more efficient.

Comments

Y
You
Commenting
0/2000
Loading comments…