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

React Lists

Last Updated : 07 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

React Lists are used to display a collection of similar data items like an array of objects and menu items. It allows us to dynamically render the array elements and display repetitive data.

Rendering List in React

To render a list in React, we will use the JavaScript array map function. We will iterate the array using map() and return the required element in the form of JSX to render the repetitive elements.

JavaScript
function App() {
    const items = ['Apple', 'Banana', 'Cherry'];

    return (
        <div>
            <h1>My Fruit List</h1>
            <ul>
                {items.map((item, index) => (
                    <li key={index}>{item}</li>
                ))}
            </ul>
        </div>
    );
}

export default App;

Output

react-lists

React Lists

In this example

  • The .map() function loops over the items array.
  • Each item is rendered inside an <li> tag.
  • The key prop should be added to uniquely identify each list item.

Note

  • Use a unique identifier like id when available.
  • Avoid using array indexes as keys unless necessary (e.g., static lists without reordering).
const users = [
    { id: 1, name: "Jay" },
    { id: 2, name: "Ajay" },
    { id: 3, name: "Vijay" }
];

List with Objects

Lists can also be created using objects where each item contains multiple properties.

JavaScript
const users = [
    { id: 1, name: 'Geeks', age: 30 },
    { id: 2, name: 'for', age: 25 },
    { id: 3, name: 'Geeks', age: 20 },
];
const App = () => {
    return (
        <ul>
            {users.map((user) => (
                <li key={user.id}>
                    {user.name} is {user.age} years old.
                </li>
            ))}
        </ul>
    );
}

export default App;

Output

Geeks is 30 years old.
for is 25 years old.
Geeks is 20 years old.

Conditional Rendering in Lists

Sometimes, you may want to render items conditionally based on certain conditions.

JavaScript
const App = () => {
    const users = [
        { id: 1, name: 'geeks', age: 30 },
        { id: 2, name: 'for', age: 25 },
        { id: 3, name: 'geeks', age: 35 },
    ];
    return (
        <ul>
            {users.map((user) => (
                user.age > 30 ? (
                    <li key={user.id}>{user.name} is over 30 years old</li>
                ) : (
                    <li key={user.id}>{user.name} is under 30 years old</li>
                )
            ))}
        </ul>
    );
};

export default App;

Output

geeks is under 30 years old
for is under 30 years old
geeks is over 30 years old

List with a Click Event

A “List with a Click Event” in React typically refers to displaying a list of items (such as an array) on the screen, where each item is associated with an event handler (such as onClick) that executes a function when the item is clicked.

JavaScript
const App = () => {
    const COMPANY = ["GEEKS", "FOR", "GEEKS"];
    const handleClick = (COMPANY) => {
        alert(`You clicked on ${COMPANY}`);
    };

    return (
        <ul>
            {COMPANY.map((COMPANY, index) => (
                <button key={index} onClick={() => handleClick(COMPANY)}>
                    {COMPANY}
                </button>
            ))}
        </ul>
    );
};
export default App;

Output

ClickEvent-1

List with click event

In this example

  • Defines a function handleClick that shows an alert with the clicked item.
  • The alert displays the name of the clicked item from the COMPANY array.
  • Starts returning JSX for rendering the list.
  • Iterates over the COMPANY array to generate a button for each item.
  • Renders a button with an onClick event that calls handleClick when clicked.

Become a Full-Stack Developer and also get 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

After successfully processing refunds worth over INR 5 Cr, GeeksforGeeks is back with the Three 90 challenge and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!


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
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.
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 prod
4 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
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
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
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:Ta
3 min read
React animated loading/splash screen using 'react-spinners'
Displaying a loading or splash screen during response time from the server is an excellent way to interact with the user. But making a loading/splash screen becomes difficult when we want to practically use an animated loader, where we need to make extra efforts to write down its styling file. To overcome this problem, we use a bunch of predefined
2 min read