×

React Lists

React Lists

Lists are used for displaying the data in an ordered format and mainly used to display the menus on websites. The creating of lists in React is as similar as we create the list in JavaScript.

The map() function is used to traverse the list. Let’s understand it with an example to see how we traverse the list in JavaScript. In the following example, we are using the map() function, which is taking an array of numbers and divides its value by 5.

The new array which is returned by the map() function is assigned to the variable named as final.

Example:

In JavaScript

var numbers = [10, 20, 30, 40, 50];

const final = numbers.map((number) => {
  return number / 5;
});

document.write(final);

Output:

After the successful compilation of the above code, you will get the following output:

React Lists

Now, let us understand the creation and traversing the list in React. For traversing the list, we will again use the map() function. At last, we will assign the array elements to the new variable final. After that we include this new list inside the <ul></ul> element.

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

const list = ['Peter', 'Sachin', 'Kevin', 'Dhoni', 'Alisa'];
const final = list.map((item) => {
  return <li key={item}>{item}</li>;
});

ReactDOM.render(
  <ul>{final}</ul>,
  document.getElementById('app')
);

export default App;

Output:

React Lists example

Rendering Lists Inside Components

The approach applied in the previous example is not a good practice for rendering the list in React because we had directly rendered the list to the DOM. We had already seen that, in React, everything is built as individual components. So, we require rendering the lists in a component.

For example:

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

function ListExample(props) {
  const lists = props.lists;
  const final = lists.map((list, index) => <li key={index}>{list}</li>);

  return (
    <div>
      <h2>Rendering Lists inside Component</h2>
      <ul>{final}</ul>
    </div>
  );
}

const lists = ['Peter', 'Sachin', 'Kevin', 'Dhoni', 'Alisa'];

ReactDOM.render(
  <ListExample lists={lists} />, 
  document.getElementById('app')
);

export default ListExample;

Output:

Rendering Lists Inside Components

Related Topics

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.

How to use React Context

React Context React Context is used to pass the data through the component tree without passing the props down manually at the every level. In React Application, the data is passed in a top-down approach...

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

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.

Difference Between State and Props in React

Comparison between State and Props 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...

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

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

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.

React Refs

React Refs The word ‘Refs’ is an abbreviation of ‘References’ in React. It is as similar as Keys in React. Refs are a function in React which is used for accessing the DOM element and...

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

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