The Wayback Machine - https://web.archive.org/web/20240930162813/https://www.geeksforgeeks.org/reactjs-lists/
Open In App

React Lists

Last Updated : 28 May, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Lists are very useful when it comes to developing the UI of any website. React Lists are mainly used for displaying menus on a website, for example, the navbar menu. In regular JavaScript, we can use arrays for creating lists. In React, rendering lists efficiently is critical for maintaining performance and ensuring a smooth user experience. React provides powerful tools to handle lists, allowing developers to create dynamic and responsive applications.

In this tutorial, we will learn about React lists with examples. We will cover React list fundamentals like creating lists, traversing lists, and rendering lists in React with examples. Let’s start by learning how to create and traverse React lists.

Rendering Lists with the map() Function

We can create lists in React just like we do in regular JavaScript i.e. by storing the list in an array. To traverse a list we will use the map() function. To create a React list, follow these given steps:

Step 1: Create a list of elements in React in the form of an array and store it in a variable. We will render this list as an unordered list element in the browser.

Step 2: We will then traverse the list using the JavaScript map() function and update elements to be enclosed between <li> </li> elements. 

Step 3: Finally we will wrap this new list within <ul> </ul> elements and render it to the DOM.

React List Examples

Example: This example implements a simple list in ReactJS.

JavaScript
// Filename -  index.js 

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

const numbers = [1,2,3,4,5];

const updatedNums = numbers.map((number)=>{
    return <li>{number}</li>;
});

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

Output: The above code will render an unordered list as shown below 

ReactJS lists

ReactJS Lists

Rendering lists inside Components

In the above code in React, we directly rendered the list to the DOM. But usually, this is not a good practice to render lists in React. We already have talked about the uses of Components and have seen that everything in React is built as individual components.

Consider the example of a Navigation Menu. It is obvious that in any website the items in a navigation menu are not hard coded. This item is fetched from the database and then displayed as a list in the browser. So from the component’s point of view, we can say that we will pass a list to a component using props and then use this component to render the list to the DOM.

We can update the above code in which we have directly rendered the list to now a component that will accept an array as props and return an unordered list. 

JavaScript
// Filename - index.js

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

// Component that will return an
// unordered list
function Navmenu(props)
{
    const list = props.menuitems;

    const updatedList = list.map((listItems)=>{
        return <li>{listItems}</li>;
    });

    return(
        <ul>{updatedList}</ul>
    );
}

const menuItems = [1,2,3,4,5];

ReactDOM.render(
    <Navmenu menuitems = {menuItems} />, 
    document.getElementById('root')
);

Output: 

ReactJS list

ReactJS Lists

The above code will give a warning in the console of the browser like:

React list Warning

ReactJS Lists Warning

The above warning message says that each of the list items in our unordered list should have a unique key.  

Using Keys in Lists

Keys are a important aspect of rendering lists in React. They help React identify which items have changed, are added, or are removed. Providing a unique key for each list item significantly improves performance and avoids potential bugs.

Example: This is the updated code for React List with keys:

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

// Component that will return an
// unordered list
function Navmenu(props)
{
    const list = props.menuitems;

    const updatedList = list.map((listItems)=>{
        return(
                <li key={listItems.toString()}>
                    {listItems}
                </li>
            ); 
    });

    return(
        <ul>{updatedList}</ul>
    );
}

const menuItems = [1,2,3,4,5];

ReactDOM.render(
    <Navmenu menuitems = {menuItems} />, 
    document.getElementById('root')
);

Output: In the below-shown output you can see the rendered output is the same but this time without any warning in the console.

ReactJS Lists with Keys

Keys are used in React to identify which items in the list are changed, updated, or deleted. We will learn about keys in more detail in our ReactJS keys article.

Conclusion

In summary, React lists are arrays with values. To render the elements of an array we iterate over each element and create a JSX element for each item. We use keys to label list elements, and when the changes are made to the list we don’t need to re-render the entire list.

This tutorial explains React lists and uses JavaScript codes to show examples of all different concepts. After completing this tutorial, you will be able to use lists in your React project. 



Previous Article
Next Article

Similar Reads

React.js Blueprint Typography Lists Right-to-left text
Blueprint is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex data-dense for desktop applications. There are different ways to design typography. The bp4-rtl class is used to make the typography move from left to right. React.js BluePrint Typography Lists Right-to-left text Cl
3 min read
React.js Blueprint Typography Lists Dark theme
BlueprintJS is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex data-dense for desktop applications. There are two color themes provided: Light and Dark. To make the typography background dark we make use of a special class provided. React.js BluePrint Typography Lists Dark th
3 min read
React.js BluePrint Typography Lists
BlueprintJS is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex and data-dense for desktop applications. Typography is used to present components efficiently. There are different ways to design typography. Typography Lists are used to display list data on the web page. React.j
3 min read
React.js Blueprint Typography Lists Internationalization
Blueprint is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex and data-dense for desktop applications. In this article, we'll discuss React.js Blueprint Typography Lists Internationalization. Typography is used to add text in various different ways to design a web page. The Re
3 min read
React MUI Lists Display
React Lists are a continuous group of text or images. They are composed of items containing primary and supplemental actions, which are represented by icons and text. Material UI is a library of React UI components that implements Google's Material Design. It includes a comprehensive collection of prebuilt components that are ready for use in produ
4 min read
How to Make Scrollable Drop Down Lists in React-Bootstrap ?
In this article, we will learn how to make Scrollable Dropdown lists in React-Bootstrap. Drop-down lists allow users to choose an option from a list of available options visible whenever the corresponding component is clicked or triggered. Using React-bootstrap, we can customize the drop-down list as needed. Syntax:<Dropdown> <Dropdown.Tog
3 min read
What is the significance of keys in React lists?
In React, when you render a list of elements using the map function or a similar approach, each rendered element must have a unique "key" prop. React key is a special string attribute that helps React identify which items have changed, been added, or been removed from the list. Here's why keys are significant: Optimizing Render Performance: React u
2 min read
How to Render Lists in React?
In React, rendering lists of data is a common task. There are multiple approaches to achieve this. In this article we will explore various approaches to render lists in React. Table of Content Using map() methodUsing forEach() methodSteps to Setup a React App:Step 1: Create a React application using the following command. npx create-react-app myapp
2 min read
How to Capture the Value of Dropdown Lists with React-Bootstrap?
Dropdown lists let users choose one or more items from a list, which makes them an important part of websites and apps. React-Bootstrap provides tools that make creating and maintaining these dropdown lists easier with a consistent look and feel. We will explore how to capture selected values from dropdowns using two approaches in React-Bootstrap:
4 min read
Create a Card View in React native using react-native-paper
React Native is a framework developed by Facebook for creating native-style apps for iOS & Android under one common language, JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can now render mobile UIs for both platforms. Approach: In this ar
2 min read
Create an Activity Indicator in react-native using react-native-paper library
React Native is a framework developed by Facebook for creating native-style apps for iOS & Android under one common language, JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can now render mobile UIs for both platforms. Prerequisites: Basic
2 min read
React Suite Icon extension React Icons
A React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React Suite Icon extension React icons. React Icons, contains various popular
3 min read
How to Add MUI React Button Icon in React-Bootstrap ?
In this article, we will learn how to add the mui react button icon in react-bootstrap. Icons can be added in the react-bootstrap by importing the MUI icon component and using it within your React-Bootstrap button. React-Bootstrap is a front-end framework that was designed keeping React in mind. Steps to Create React Application And Installing Modu
2 min read
How to import from React-Select CDN with React and Babel ?
In ReactJS, import from React-Select CDN is not done in a simple way as we do with the npm packages. To use them we need to use directly them in the HTML file. It does not work with the project set-up by creating a react app. Prerequisites:React JSHTML, CSS, JavaScriptSteps to Import React-Select CDN with React and Babel:Step 1: Create index.html w
2 min read
Explain new features of React Memo in React v16.6 ?
React memo is a Higher Order Component (HOC) introduced in React v16.6. As the name suggests, Memo API uses the memoization technique to optimize performance and make the application faster. The Memo API avoids unnecessary re-renders in functional components thereby optimizing the performance of the application. We will see two different examples m
2 min read
What is the difference between react-fetch and whatwg-fetch in React.js ?
React-fetch and Whatwg-fetch are both libraries for making HTTP requests in a React application, but they are used for different purposes and have different features. Table of Content React-fetchWhatwg-fetchDifference between React-fetch and Whatwg-fetchReact-fetch: `react-fetch` is a library offering a higher-order component (HOC) for streamlined
2 min read
Plugin "react" was conflicted between "package.json » eslint-config-react-app
The error Plugin "react" was conflicted between package.json and eslint-config-react-app generally arises when there is a conflict in naming convention or package.json data. However, this is not the only reason for this conflict. Some of the other reasons can be due to a different version or conflict in project dependencies. Error image from Termin
2 min read
How to Zoom in/Zoom out using React-PDF in React JS ?
Zooming in or out in React-PDF within a React JS application is achieved by using the 'scale' prop inside the `<Page />` component. The 'scale' prop accepts decimal values; values greater than 1 zoom in, while values less than 1 zoom out the page. Prerequisites:React JSReact Functional ComponentsSyntax: <page scale = {value} /> // When
2 min read
Why to use React Hooks Instead of Classes in React JS ?
The introduction of React Hooks has changed the way we are managing states and lifecycle features. They offer more easy and functional way as compared to class based components. In this article, we will learn why to use React Hooks Instead of Classes in ReactJS, but lets first discuss about both React hooks and class based components. Table of Cont
4 min read
What are React Hooks, and why were they added to React?
React Hooks are a way to add functionality to functional components in React. Before Hooks, functional components were more limited compared to class components in terms of what they could do. With Hooks, users can now use state, lifecycle methods, and other React features in functional components, making them more powerful and flexible. Hooks Addr
2 min read
Difference between React.memo() and useMemo() in React.
In React applications, optimizing performance is crucial for delivering a smooth user experience. Two key tools for achieving this are React.memo() and useMemo(). While both help improve performance, they serve different purposes and are used in distinct scenarios. Table of Content What is React.memo() ?What is useMemo() ?Difference Between React.m
3 min read
How does React.memo() optimize functional components in React?
React.memo() is a higher-order component provided by React that can help optimize functional components by reducing unnecessary re-renders. Let's explore how it works and how it optimizes functional components: Table of Content Optimizing with React.memo()When to use React.memo() ?Benefits of memoization using React.memo()How to use React.memo() ?C
4 min read
How to add a divider in react-native using react-native-paper library ?
React native is a framework developed by Facebook for creating native-style apps for iOS & Android under one common language, JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can now render mobile UI's for both platforms. Prerequisites: Basi
2 min read
How does React Router handle navigation in a React application?
React Router is a standard library for routing in React. It enables the navigation among views of various components in a React Application, allows changing the browser URL, and keeps the UI in sync with the URL. Let us create a simple application to React to understand how the React Router works React Router simplifies navigation in React applicat
3 min read
What is the use of React Context in React-Redux?
React Context is a feature that React provides us to manage states required in multiple components. Redux is also a state management library and solves the same problem that React Context does but in a different way. In this article, we will see in detail what is react context, why and how to use it, how it is related to and different from react-re
5 min read
Implementing React Router with Redux in React
This article explores implementing React Router with Redux for enhanced state management and routing in React applications. Table of Content What is React Router and Redux?Approach to implement React Router in ReduxSteps to Setup the React AppWhat is React Router and Redux?React Router facilitates routing in React apps, managing component rendering
3 min read
Implementation of React.memo() and useMemo() in React
React.memo allows functional component to have same optimization as Pure Component provides for class-based components. useMemo is for memoizing function's calls inside functional component. What is React.memo() ?React.memo() is a higher-order component (HOC) provided by React that memoizes functional components. It means that it caches the result
3 min read
What are the React Router hooks in React v5?
React Router hooks perform client-side single-page routing that provides a functional and streamlined approach to managing navigation in React applications. It provides a way to directly access the state of the router from functional components, this simplifies tasks like retrieving information about the desired URL and navigating through web pages
5 min read
How to Fix React useEffect running twice in React 18 ?
In React applications, the useEffect hook is commonly used for handling side effects, such as data fetching, subscriptions, or manually changing the DOM. However, encountering the issue of useEffect running twice can lead to unexpected behavior and impact the performance of your application. In this article, we'll explore various approaches to reso
4 min read
How to create Chip in react-native using react-native-paper ?
React native is a framework developed by Facebook for creating native-style apps for iOS & Android under one common language, JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can now render mobile UI's for both platforms. Prerequisites: Basi
2 min read