Component Collection
Rendering Lists in React
In modern applications, developers often need to render a list of items—such as todos, orders, invoices, or expenses—in tabular or gallery formats. React provides a simple, intuitive, and efficient approach to creating list-based user interfaces.
React mainly uses two existing features to achieve this:
JavaScript’s built-in map() method
React expressions inside JSX
JavaScript map() Method
The map() function works on collections such as arrays. It applies a function to each item in the array and returns a new array with the transformed values.
Example
let list = [10, 30, 45, 12, 24]; let result = list.map((input) => input * 2);
Output
[20, 60, 90, 24, 48]
React Expressions in JSX
JSX allows embedding JavaScript expressions inside {}.
Example
var hello =
Hello!
; var final =Generated Output
Hello!
Example: Rendering a List Using map()
Let us apply these concepts to create a component that displays expense items in a table format.
Step 1: Project Setup
Open your expense-manager application in your editor.
Create the following files inside src/components:
ExpenseEntryItemList.css
ExpenseEntryItemList.js
Step 2: Import React and CSS
import React from 'react'; import './ExpenseEntryItemList.css';
Step 3: Create the Component Class
class ExpenseEntryItemList extends React.Component { constructor(props) { super(props); } render() { } }
Step 4: Generate Table Rows Using map()
render() { const lists = this.props.items.map((item) =>
Note:
Each item must have a unique key so React can efficiently track changes in the list.
Step 5: Render the Table
return (
| Item | Amount | Date | Category |
|---|
Step 6: Export the Component
export default ExpenseEntryItemList;
Complete Component Code
import React from 'react'; import './ExpenseEntryItemList.css'; class ExpenseEntryItemList extends React.Component { render() { const lists = this.props.items.map((item) =>
| Item | Amount | Date | Category |
|---|
Step 7: Update index.js
Import the Component
import ExpenseEntryItemList from './components/ExpenseEntryItemList';
Create Expense Data
const items = [ { id: 1, name: "Pizza", amount: 80, spendDate: "2025-10-10", category: "Food" }, { id: 2, name: "Grape Juice", amount: 30, spendDate: "2025-10-12", category: "Food" }, { id: 3, name: "Cinema", amount: 210, spendDate: "2025-10-16", category: "Entertainment" }, { id: 4, name: "Java Programming book", amount: 242, spendDate: "2025-10-15", category: "Academic" }, { id: 5, name: "Mango Juice", amount: 35, spendDate: "2025-10-16", category: "Food" }, { id: 6, name: "Dress", amount: 2000, spendDate: "2025-10-25", category: "Cloth" }, { id: 7, name: "Tour", amount: 2555, spendDate: "2025-10-29", category: "Entertainment" }, { id: 8, name: "Meals", amount: 300, spendDate: "2025-10-30", category: "Food" }, { id: 9, name: "Mobile", amount: 3500, spendDate: "2025-11-02", category: "Gadgets" }, { id: 10, name: "Exam Fees", amount: 1245, spendDate: "2025-11-04", category: "Academic" } ];
Render the Component
import React from 'react';
import { createRoot } from 'react-dom/client';
import ExpenseEntryItemList from './components/ExpenseEntryItemList';
const container = document.getElementById('root');
const root = createRoot(container);
root.render(
Styling (ExpenseEntryItemList.css)
html { font-family: sans-serif; } table { border-collapse: collapse; border: 2px solid rgb(200,200,200); letter-spacing: 1px; font-size: 0.8rem; } td, th { border: 1px solid rgb(190,190,190); padding: 10px 20px; text-align: left; } th { background-color: rgb(235,235,235); } tr:nth-child(even) td { background-color: rgb(250,250,250); } tr:nth-child(odd) td { background-color: rgb(245,245,245); }
Run the Application
npm start
Open the browser at:
http://localhost:3000
Output
Finally, open the browser and enter http://localhost:3000 in the address bar and press enter.
| Item | Amount | Date | Category |
|---|---|---|---|
| Pizza | 80 | Sat Oct 10 2025 | Food |
| Grape Juice | 30 | Man Oct 12 2025 | Food |
| Cinema | 210 | Fri Oct 16 2025 | Entertainment |
| Java Programming book | 242 | Thu Oct 15 2025 | Academic |
| Mango Juice | 35 | Fri Oct 16 2025 | Food |
| Dress | 2000 | Sun Oct 25 2025 | Cloth |
| Tour | 2555 | Thu Oct 29 2025 | Entertainment |
| Meals | 300 | Fri Oct 30 2025 | Food |
| Mobile | 3500 | Mon Nov 02 2025 | Gadgets |
| Exam Fees | 1245 | Wed Nov 04 2025 | Academic |