List all ways to render a list of items in React
Last Updated :
23 Jul, 2025
In React applications, rendering lists of items is a fundamental task. You will often need to display collections of data, such as product listings, to-do items, or user comments. There is well-established approach that combines JavaScript's array methods and React's component structure to achieve this efficiently.
Pre-requisites:
Steps to Create an React Application
Step 1: Create a React application using the following command and navigate to it.
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Step 2: Install required dependencies.
npm i react react-dom
Updated dependencies in package.json file
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
},
Project Structure:
Project Structure The following are the ways to implement or render list of items using React component.
Common Code: The following "app.js" code will be common for all other codes from where the other codes are included as <ListComponent>
JavaScript
// app.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import ListComponent from './ListComponent';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<div style={{ display:'flex', flexDirection:'column', alignItems:'center' }}>
<div>
<img src=
'https://media.geeksforgeeks.org/gfg-gg-logo.svg' alt='gfg_logo' />
</div>
<ListComponent />
</div>
);
1. Using Array.map
This approach is the most common and idiomatic method for rendering lists in React. It leverages the map() method available on JavaScript arrays to iterate over each item and generate corresponding JSX elements. Each item in the array is transformed into a React component or element, facilitating the rendering process. This <ListComponent> is included in the above common code and it is run to give the output.
Example:
JavaScript
import React from 'react';
function ListComponent() {
// Sample array of items
const items = ['Apple', 'Banana',
'Orange', 'Papaya', 'Guava',
'Grapes', 'Date'];
// Rendering the list using Array.map()
const itemList = items.map((item, index) => (
<li key={index}>{item}</li>
));
// Rendering the list within an unordered list element
return (
<div>
<h2>Fruit Name</h2>
<ul>{itemList}</ul>
</div>
);
}
export default ListComponent;
Output:
List By Array.map 2. Using a for loop
Using a for loop to render a list in React involves manually iterating over the array of items and creating JSX elements for each item within the loop. This approach provides more control over the rendering process compared to using Array.map() but may result in more verbose code.
Example:
JavaScript
import React from 'react';
function ListComponent() {
// Sample array of items
const items = ['Sunflower', 'Marigold', 'Rose', 'Jasmine', 'Hibiscus'];
// Array to store JSX elements
const itemList = [];
// Using a for loop to iterate over the array of items
for (let i = 0; i < items.length; i++) {
// Creating JSX element for each item and pushing it into the array
itemList.push(<li key={i}>{items[i]}</li>);
}
// Rendering the list within an unordered list element
return (
<div>
<h2>Flower Name</h2>
<ul>{itemList}</ul>
</div>
);
}
export default ListComponent;
Output:
List By For Loop3. Using a forEach loop
Using a forEach loop to render a list in React involves iterating over the array of items using on JavaScript arrays. Within the loop, JSX elements are created for each item, and these elements can be processed as required, such as pushing them into an array or directly rendering them.
Example:
JavaScript
import React from 'react';
function ListComponent() {
// Sample array of items
const items = ['Tomato', 'Beans',
'Pumpkin', 'Cauliflower',
'Broccoli'];
// Array to store JSX elements
const itemList = [];
// Using a forEach loop to iterate over the array of items
items.forEach((item, index) => {
// Creating JSX element for each item
// and pushing it into the array
itemList.push(<li key={index}>{item}</li>);
});
// Rendering the list within an unordered list element
return (
<div>
<h2>Vegetable Names </h2>
<ul>{itemList}</ul>
</div>
);
}
export default ListComponent;
Output:
List By For Each Loop4. Using JSX directly in the render() method
Using JSX directly in the render() method involves embedding JSX elements directly within the return statement of a React component without using any iteration or loop. This approach is suitable for rendering static lists or a small number of items where manually creating JSX elements is feasible.
Include the ListComponent in the above common code.
Example:
JavaScript
import React from 'react';
function ListComponent() {
// Rendering the list directly within the return statement
return (
<div>
<h2>Animal Name </h2>
<ul>
<li>Horse</li>
<li>Ass</li>
<li>Lion</li>
<li>Dog</li>
<li>Wolf</li>
<li>Bear</li>
<li>Tiger</li>
</ul>
</div>
);
}
export default ListComponent;
Output:
List By Directly JSX 5. Using React.Children.map
Using React.Children.map() with React Fragments allows developers to iterate over children elements within a React Fragment. This approach is useful for rendering multiple elements without introducing an extra DOM element, such as a <div>, as a container. It provides flexibility in organizing and rendering complex UI structures while maintaining a clean and concise JSX syntax.
Include the ListComponent in the above common "App.js" code.
Example:
JavaScript
import React from 'react';
function ListComponent() {
// Sample array of items
const items = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'];
// Rendering the list using React.Children.map() with React Fragments
return (
<div>
<h2>Item List </h2>
{React.Children.map(items, (item, index) => (
<li key={index}>{item}</li>
))}
</div>
);
}
export default ListComponent;
Output:
List By React.Children.Map6. Using a custom component
Creating a custom component in React involves defining a reusable function or class component to encapsulate specific functionality. This custom component can accept props to customize its behavior and appearance. Using custom components enhances code readability, promotes reusability, and simplifies the maintenance of React applications.
Example:
JavaScript
import React from 'react';
// Custom List
function List({ name }) {
return (
<li>{name}</li>
);
}
function ListComponent({ items }) {
const birds = ['Peacock', 'Bird', 'Pigeons',
'Hummingbird', 'Flamingo', 'Crow',
'Eagle', 'Parrot']
return (
<div>
<h2>Bird Name</h2>
<ul>
{birds.map((name, index) => (
<List name={name} key={index} />
))}
</ul>
</div>
);
}
export default ListComponent
Output:
List By Custom Component
Similar Reads
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability.React.jsWhy Use React?Before React, web development faced issues like slow DOM updates
7 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDOM is a core React package that provides methods to interact with the Document Object Model, or DOM. This package allows developers to access and modify the DOM. It is a package in React that provides DOM-specific methods that can be used at the top level of a web app to enable an efficient wa
3 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsReact 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 ReactTo render a list in React, we will use the JavaScript array map() function. We will ite
5 min read
React FormsForms are an essential part of any application used for collecting user data, processing payments, or handling authentication. React Forms are the components used to collect and manage the user inputs. These components include the input elements like text field, check box, date input, dropdowns etc.
5 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. In this article, we'll explore ReactJS keys, understand their importance, how the
5 min read
Components in React
React ComponentsIn React, React components are independent, reusable building blocks in a React application that define what gets displayed on the UI. They accept inputs called props and return React elements describing the UI.In this article, we will explore the basics of React components, props, state, and render
4 min read
ReactJS Functional ComponentsIn ReactJS, functional components are a core part of building user interfaces. They are simple, lightweight, and powerful tools for rendering UI and handling logic. Functional components can accept props as input and return JSX that describes what the component should render.Stateless (before hooks)
5 min read
React Class ComponentsClass components are ES6 classes that extend React.Component. They allow state management and lifecycle methods for complex UI logic.Used for stateful components before Hooks.Support lifecycle methods for mounting, updating, and unmounting.The render() method in React class components returns JSX el
4 min read
ReactJS Pure ComponentsReactJS Pure Components are similar to regular class components but with a key optimization. They skip re-renders when the props and state remain the same. While class components are still supported in React, it's generally recommended to use functional components with hooks in new code for better p
4 min read
ReactJS Container and Presentational Pattern in ComponentsIn this article we will categorise the react components in two types depending on the pattern in which they are written in application and will learn briefly about these two categories. We will also discuss about alternatives to this pattern. Presentational and Container ComponentsThe type of compon
2 min read
ReactJS PropTypesIn ReactJS PropTypes are the property that is mainly shared between the parent components to the child components. It is used to solve the type validation problem. Since in the latest version of the React 19, PropeTypes has been removed. What is ReactJS PropTypes?PropTypes is a tool in React that he
5 min read
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects