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 <tr>

 

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.

Be it a software developer, programmer, coder, or a consultant, CronJ has it all. CronJ has been a trustworthy company for startups, small companies, and large enterprises. Hire the web of experienced React developers for your esteemed project today. ReactJS Development Services