×

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

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

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

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.

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.

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.

React Component API

React Component API It is a top-level API. It provides reusability to the code in the application and makes it completely individual. It has several methods for: Creating Elements.Transforming Elements.Fragments. Now, we are explaining the three...

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.

React Forms

React Forms Forms are the important part of any web application. It allows the interaction of the user with the app as well as the gathering of the information from the...

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

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.