TheDeveloperBlog.com

Home | Contact Us

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

React Lists

React Lists 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 Lists

Lists are used to display data in an ordered format and mainly used to display menus on websites. In React, Lists can be created in a similar way as we create lists in JavaScript. Let us see how we transform Lists in regular JavaScript.

The map() function is used for traversing the lists. In the below example, the map() function takes an array of numbers and multiply their values with 5. We assign the new array returned by map() to the variable multiplyNums and log it.

Example

var numbers = [1, 2, 3, 4, 5]; 
const multiplyNums = numbers.map((number)=>{ 
	return (number * 5); 
}); 
console.log(multiplyNums); 

Output

The above JavaScript code will log the output on the console. The output of the code is given below.

[5, 10, 15, 20, 25]

Now, let us see how we create a list in React. To do this, we will use the map() function for traversing the list element, and for updates, we enclosed them between curly braces {}. Finally, we assign the array elements to listItems. Now, include this new list inside <ul> </ul> elements and render it to the DOM.

Example

import React from 'react'; 
import ReactDOM from 'react-dom'; 

const myList = ['Peter', 'Sachin', 'Kevin', 'Dhoni', 'Alisa']; 
const listItems = myList.map((myList)=>{ 
	return 
  • {myList}
  • ; }); ReactDOM.render(
      {listItems}
    , document.getElementById('app') ); export default App;

    Output

    React Lists

    Rendering Lists inside components

    In the previous example, we had directly rendered the list to the DOM. But it is not a good practice to render lists in React. In React, we had already seen that everything is built as individual components. Hence, we would need to render lists inside a component. We can understand it in the following code.

    Example

    import React from 'react'; 
    import ReactDOM from 'react-dom'; 
    
    function NameList(props) {
      const myLists = props.myLists;
      const listItems = myLists.map((myList) =>
        
  • {myList}
  • ); return (

    Rendering Lists inside component

      {listItems}
    ); } const myLists = ['Peter', 'Sachin', 'Kevin', 'Dhoni', 'Alisa']; ReactDOM.render( , document.getElementById('app') ); export default App;

    Output

    React Lists
    Next TopicReact Keys




    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