×

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

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

React Animation The animation is a procedure in which an image is manipulated to appear as a moving image. It is widely used to create an interactive web application. In React, we have to...

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

What is React Router? Routing is a process by which a user can direct to different pages based on their actions and requests. ReactJS router is generally used to develop single...

10 minutes read.

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

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.

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

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.

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.

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.

React Higher-Order Components

React Higher-Order Components In short form, Higher-Order Components are represented as HOC. The Higher-order component is an advanced technique to use component logic. HOC is a function that takes the component and returns the new...

2 minutes read.