×

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

What is React CSS React CSS is used to provide the style to the React applications. The style attribute adds dynamically-computed styles at render time, and it is one of the most used...

5 minutes read.

React Redux

How to Use Redux with React Hooks It is an open-source library of JavaScript which is used for managing the application state. React uses the Redux to build the user interface....

5 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 Events

React Events An event is an action that triggers a response of the user action or system-generated event. As HTML, React can also perform actions based on user events. React has the...

2 minutes read.

React Map

React Map The map() is the standard function of JavaScript, which can be called at any array. The map() method is used for traversing and displaying a list of the similar objects of...

2 minutes read.

React Environment Setup Step by Step

React Environment Setup This section will provide you the information about how to set up an environment for the successful development of ReactJS application: Pre-Requisite for ReactJS NodeJS and NPMReact and React DOMWebpackBabel There are two...

5 minutes read.

React Components

React Components Former, the developers used to write more than thousands of lines of code to develop a single-page application. These applications follow the structure of traditional DOM, and it was very challenging to make...

2 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.

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.

React State

React State The state is an updatable structure which is used for containing data or information about the component. The state in a component can be changed over time. The change...

3 minutes read.

React Constructors

What is constructor? The constructor is a method that is used for initializing the state of the object in the class. It calls automatically during the creation of an object in...

4 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.

Pros and Cons of ReactJS

Pros and Cons of ReactJS There are various advantages, and disadvantages of ReactJS are as follows: Benefits of ReactJS 1. Easy to Learn and Use: ReactJS is very easy to use and learn. It has a good...

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 Flux

React Flux Introduction It is an application architecture that is internally used by Facebook to build the client-web application with react. It is useful when the project includes dynamic data, and...

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.

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 Table

React Table The table represents an arrangement that organizes the information in the form of rows and columns. The table is mainly used for storing and displaying the data within a structured format. Features of...

2 minutes read.

React Component Life-Cycle

React Component Life-Cycle In ReactJS, the creation process of every component includes several lifecycle methods. These methods, together stated as component's lifecycle. Four phases of the component's life cycle are as...

4 minutes read.