TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

React Fragments

React Fragments with ReactJS Tutorial, ReactJS Introduction, ReactJS Features, ReactJS Installation, Pros and Cons of ReactJS, AngularJS vs ReactJS, Reactnative vs ReactJS, ReactJS Router, ReactJS Flux Concept, ReactJS Animations, ReactJS Discussion, ReactJS Quick Guide, etc.

<< Back to REACT

React Fragments

In React, whenever you want to render something on the screen, you need to use a render method inside the component. This render method can return single elements or multiple elements. The render method will only render a single root node inside it at a time. However, if you want to return multiple elements, the render method will require a 'div' tag and put the entire content or elements inside it. This extra node to the DOM sometimes results in the wrong formatting of your HTML output and also not loved by the many developers.

Example

// Rendering with div tag
class App extends React.Component { 
     render() {  
	  return ( 
	     //Extraneous div element 
	     

Hello World!

Welcome to the JavaTpoint.

); } }

To solve this problem, React introduced Fragments from the 16.2 and above version. Fragments allow you to group a list of children without adding extra nodes to the DOM.

Syntax


      

child1

child2

.. ..... .... ...

Example

// Rendering with fragments tag
class App extends React.Component { 
    render() { 
	 return ( 
	   
            

Hello World!

Welcome to the JavaTpoint.

); } }

Why we use Fragments?

The main reason to use Fragments tag is:

  1. It makes the execution of code faster as compared to the div tag.
  2. It takes less memory.

Fragments Short Syntax

There is also another shorthand exists for declaring fragments for the above method. It looks like empty tag in which we can use of '<>' and '' instead of the 'React.Fragment'.

Example

//Rendering with short syntax 
class Columns extends React.Component { 
  render() { 
    return ( 
      <>  
        

Hello World!

Welcome to the JavaTpoint

); } }

Keyed Fragments

The shorthand syntax does not accept key attributes. You need a key for mapping a collection to an array of fragments such as to create a description list. If you need to provide keys, you have to declare the fragments with the explicit <React.Fragment> syntax.

Note: Key is the only attributes that can be passed with the Fragments.

Example

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

{item.name}

{item.url}

{item.description}

))}
) }

Next TopicReact Router




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf