Are React Fragments worth using? – Find the Use Cases

Are React Fragments worth using? – Find the Use Cases

React Fragments: As per React docs, a common pattern in React is for components to return multiple elements. Usually, these elements need to be wrapped in an element div. In most of these cases, the wrapper div is irrelevant and is only added...

React Fragments: As per React docs, a common pattern in React is for components to return multiple elements. Usually, these elements need to be wrapped in an element div. In most of these cases, the wrapper div is irrelevant and is only added because React components support the return of only one element. This kind of behavior results in useless markup and sometimes even invalid HTML to be rendered, which is bad.

Let’s say, we have a component Table that renders an HTML table and inside that table, the columns are rendered with another component called Columns. It would probably look something like this –

react fragments

This would result in an invalid HTML to be rendered because the wrapper div from Columns component is rendered inside the

reactjs fragments table

For this problem, React in version 16.2.0 introduced the concept of Fragments. React fragments are used to group a list of children without adding extra nodes to the DOM because fragments are not rendered to the DOM. So basically we can use React.Fragment where we would normally use a wrapper div.

We can make use of fragments with <React.Fragments> syntax. Now, Column component can be written as

react fragments class column extensds

Now the Table component would render the following HTML

react table

Fragments can also be declared with a short syntax which looks like an empty tag that is <> and </>

Typical use cases

1) Return multiple elements

The most common use case for React fragments is probably to return multiple elements.

claass application extends

2) Conditional rendering

React fragments can be used when elements are rendered conditionally. They can make rendering groups of elements a lot easier as they remove the use of extra div elements.

class demo extends react component

3) To Render Arrays

Fragments can also help when rendering arrays because fragments can have keys. Let’s say there is an array of user objects and all the users are to be rendered. A key prop has to b set for every user, in this case, to avoid a warning from React. So Fragments can be used in such cases as well.

class userlist extends react component

Key is the only prop that can be passed to a React Fragment. Also, note that the short syntax <></> of Fragments does not support keys.

Should fragments be used? So are fragments worth using? Here are the few reasons why they are –

It’s a bit faster and has less memory usage as compared to an extra div (no need to create an extra DOM node). This includes a real benefit on very large applications with very deep or large trees in DOM, but application performance often suffers from death by 1000 cuts. This can be one cut less. Flex, CSS Grids, and a few other CSS mechanisms have a special parent-child relationship, and adding divs in the middle makes it hard to keep the specified layout while extracting logical components. The DOM inspector is less cluttered.

React js Development Company

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Are React Fragments worth using? – Find the Use Cases

React Fragments: As per React docs, a common pattern in React is for components to return multiple elements. Usually, these elements need to be wrapped in an element div. In most of these cases, the wrapper div is irrelevant and is only added...

Are React Fragments worth using? – Find the Use Cases

Hire Reactjs development company, React js development company, React development company , React.js development company, reactjs development companies, react.js development companies, react js development companies from India and USA.

What are hooks in React JS? - INFO AT ONE

In this article, you will learn what are hooks in React JS? and when to use react hooks? Also, we will see the react hooks example.

How to Select and Hire the Best React JS and React Native Developers?

Hire React JS developer from Technoduce, we have a team of experienced and expert Angular JS developer. With our hire React JS developer, you have complete control over the technical resource throughout the development process.