×

React Fragments

React Fragments

The fragments in React are introduced from the 16.2 and above versions. Fragments allow you to group a list of child elements without adding any extra node in the DOM.

In React, if you want to render something to the screen, you have to use a render method within the component; and this render method will render a single root node in it at a time. But if you want to return multiple statements, then we have to put the <div> tag inside the render method and need to put the entire content and elements in it. This extra node in the DOM sometimes results in the wrong formatting of your HTML output. To solve it, React introduced the concept of Fragments.

Syntax:

 

child1

child2

……………..

Example

Without Using Fragment

import React, { Component } from 'react'; 
 import { render } from 'react-dom'; 
 class App extends React.Component { 
  render() { 
  return ( 
  //Extraneous div element
  

Example of Without Using Fragment

Hello World!!.

); } } export default App;

Output:

React Fragments

With Fragment

import React, { Component } from 'react'; 
 import { render } from 'react-dom'; 
 class App extends React.Component { 
  render() { 
  return ( 
  //Fragment tag
   
  

Example of Using Fragment

Hello World!!.

); } } export default App;

Output:

With Fragment

Why Fragments

There are the following reasons that make fragments valuable.

  • It makes the code execution faster as compared to the <div> tag.
  • It consumes less memory.

Fragments short Syntax

The main reason for using the fragments is that it is a bit faster when compared with the <div> tag.

Fragment short syntax is the shorthand for declaring the fragments. It is like an empty tag. Instead of using React.Fragment, we use <>....</> for declaring the fragments.

Example:

The following example is showing how to use fragments short syntax.

import React, { Component } from 'react'; 
 import { render } from 'react-dom'; 
 class App extends React.Component { 
  render() { 
  return ( 
  //Fragment short syntax tag
  <>  
  

Fragment Short syntax Example

Hello World!! .

); } } export default App;

Output:

Fragments short Syntax

Keyed Fragments

The shorthand syntax of the fragment doesn't accept the key attributes. You require a key to map a collection to an array of fragments like to create a description list. If you want to provide keys, you need to declare the fragments with explicit <React.Fragment> syntax.

The key is the only attribute that can be passed with the fragments.

Function = (props) { 
  return ( 
   
  {props.items.data.map(item => ( 
  // Without the 'key', React will give a key warning 
   
  

{item.name}

{item.url}

{item.description}

))}
) }

Related Topics

React Router

What is React Router? Routing is a process by which a user can direct to different pages based on their actions and requests. ReactJS router is generally used to develop single...

10 minutes read.

React Conditional Rendering

React Conditional Rendering In React, the working of the conditional rendering is similar to the condition works in JavaScript. We use JavaScript operators for creating elements that represent the current state, and then the React...

3 minutes read.

React Props Validation

React Props Validation As we know, props are a suitable mechanism for passing the read-only attributes to react components. The props are required to be correctly used in the component. If it is...

5 minutes read.

React Bootstrap

React Bootstrap React has a widely used JavaScript framework for creating web applications, and Bootstrap has become the most popular CSS framework. Single-page apps have become popular from the last few...

9 minutes read.

React Keys

React Keys A key can be defined as a unique identifier. In React, it is used for identifying the items that have changed, deleted, and updated from the lists. React Keys are helpful...

3 minutes read.

Features of ReactJS

ReactJS Features As of now, within the developers, ReactJS is gaining much popularity as the best JavaScript framework. It is crucial for the front-end ecosystem. There are some of the crucial features of ReactJS that are...

2 minutes read.

React Fragments

React Fragments The fragments in React are introduced from the 16.2 and above versions. Fragments allow you to group a list of child elements without adding any extra node in the DOM. In React,...

2 minutes read.

Difference Between React Flux and MVC

React Flux vs MVC MVC MVC is an acronym of 'Model View Controller.' It is the architectural pattern that is used to develop the user interface. It has three different logical components: The...

2 minutes read.

ReactJS Tutorial

ReactJS Introduction React Tutorial helps you to understand the basic and some advanced concepts of ReactJS. As of now, it is the essential front-end library that is developed by a...

3 minutes read.

React Native vs ReactJS

Difference between React Native and ReactJS React Native React Native is also an open-source JavaScript framework that is used for the development of a mobile application for iOS Android and Windows. It...

6 minutes read.

React Code Splitting

React Code Splitting React Code Splitting is the procedure of splitting the bundle files by which the files get easily loaded on the webpage. The react application bundles the files by using...

4 minutes read.

React Portals

React Portals React portals were introduced by React 16.0 in September 2017. It gives you a way to render the element outside of the component hierarchy, i.e., within a separate component. Before React 16.0, it...

3 minutes read.

React Hooks

React Hooks The Hooks were introduced in React 16.8. By using React Hooks, you can use the state and other features of the React without writing a class. Hooks do not work within the classes....

5 minutes read.

Comparison between React and Vue

Comparison between React and Vue React, and Vue both are the two most famous libraries of JavaScript that are used for creating thousands of websites today. The React and Vue both...

3 minutes read.

React Error Boundaries

React Error Boundaries In the past, the errors of JavaScript within the components were used to corrupt the internal state of react and cause it to emit the cryptic errors on the next...

3 minutes read.

React Props

React Props A prop is an abbreviation of ‘properties.' State and props are mainly different from each other because props are immutable. Props are the read-only components. It is the object that stores the attribute...

3 minutes read.

Difference between Controlled and Uncontrolled component in ReactJs

Controlled Component In the controlled component, the input of the form element is handled by the component rather than the DOM. Controlled components have the functions that govern the data, which...

2 minutes read.

ReactJS Versions

ReactJS Versions The Complete set of release history of ReactJS is elaborated as follows. The complete set of full documentation of recent releases is on GitHub. S.noVersionRelease DateExplanation1.0.3.029/05/2013Initially released for public.2.0.4.029/07/2013Supporting comments...

4 minutes read.

React Animation

React Animation The animation is a procedure in which an image is manipulated to appear as a moving image. It is widely used to create an interactive web application. In React, we have to...

4 minutes read.

ReactJS vs AngularJS

Difference between ReactJS and AngularJS AngularJS AngularJS is the JavaScript framework that is open source, and it is also used to build a dynamic web application. It is developed in 2009 by...

5 minutes read.