How to Map Data into Components using ReactJS?
Last Updated :
09 Jan, 2025
Mapping data in react js component is a comman practice to render the lists and repeating elements. In this article, we will have a users data and we will map it to react components to display the users information in the react app
Prerequisites:
Approach
To map data in components we will be using JavaScript's Array map method. We will define Info component that takes data as a prop. Iterate the user data and pass it to info component as prop and return the required Users list.
We can also map the data manually to the components.
Steps to Map data into React Component
Step 1: Initialize React Project
npx create-react-app users-list
Step 2: Swtich to project directory
cd users-list
Project Structure:
Project StructureList of Dependencies in the package.json file
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
Step 3: Prepare the data
Store the data in array of objects in key-value pairs as given below:
JavaScript
// Filename - src/list.js
const Users = [
{
name: "Deepak",
rollNo: "123"
},
{
name: "Yash",
rollNo: "124"
},
{
name: "Raj",
rollNo: "125"
},
{
name: "Rohan",
rollNo: "126"
},
{
name: "Puneet",
rollNo: "127"
},
{
name: "Vivek",
rollNo: "128"
},
{
name: "Aman",
rollNo: "129"
}
];
export default Users;
Step 4: Create Info Component
Define a component (Info.js) that takes the name and rollno as prop and render them on UI.
JavaScript
// Filename - src/Info.js
import React from "react";
function Info(props) {
return (
<div
style={{ border: "1px solid black", margin: "10px", width: "20%" }}
>
<p> {props.name} </p>
<p> {props.rollNo}</p>
</div>
);
}
export default Info;
Step 5: Map the Data into React Component
Import the users list and info component and map the data using array map, passing the required props to Info component.
Example 1: This example uses array map method to iterate the users and passes data as props to render the list of users.
JavaScript
// Filename - src/App.js
import React from "react";
import "./App.css";
import Users from "./list";
import Info from "./Info";
function App() {
return (
<div style={{ margin: "40px" }}>
<h1 style={{ color: "green" }}> Geeks For Geeks </h1>
<p> Mapping Data to component in React js. </p>
{Users.map((e) => {
return <Info name={e.name} rollNo={e.rollNo} />;
})}
</div>
);
}
export default App;
Step 6: Run the Application
Use the below command to run the application.
npm start
Output :

Example 2: This example uses manual iterations to map the data to Info Component.
JavaScript
// Filename - src/App.js
import React from "react";
import "./App.css";
import Users from "./list";
import Info from "./Info";
function App() {
return (
<div style={{ margin: "40px" }}>
<h1 style={{ color: "green" }}> Geeks For Geeks </h1>
<p> Mapping Data to component in React js. </p>
<Info name={Users[0].name} rollNo={Users[0].rollNo} />
<Info name={Users[1].name} rollNo={Users[1].rollNo} />
<Info name={Users[2].name} rollNo={Users[2].rollNo} />
<Info name={Users[3].name} rollNo={Users[3].rollNo} />
<Info name={Users[4].name} rollNo={Users[4].rollNo} />
<Info name={Users[5].name} rollNo={Users[5].rollNo} />
<Info name={Users[6].name} rollNo={Users[6].rollNo} />
</div>
);
}
export default App;
Output:

From both the above example, we get the same output but one can observe by using the map function in the App.js file, mapping array data to component becomes more easy and less code is needed.
Similar Reads
How to use DataGrid Component in ReactJS ? A DataGrid Component helps in displaying the information in a grid-like format of rows and columns. Material UI for React has this component available for us, and it is very easy to integrate. We can use the following approach in ReactJS to use DataGrid Component.Creating React Application And Insta
2 min read
How to create components in ReactJS ? Components in React JS is are the core of building React applications. Components are the building blocks that contains UI elements and logic, making the development process easier and reusable. In this article we will see how we can create components in React JS. Table of Content React Functional C
3 min read
How to pass data into table from a form using React Components ? React JS is a front-end library used to build UI components. This article will help to learn to pass data into a table from a form using React Components. This will be done using two React components named Table and Form. We will enter data into a form, which will be displayed in the table on 'submi
3 min read
How to use List Component in ReactJS? Lists are continuous, vertical indexes of text or images. Material UI for React has this component available for us, and it is very easy to integrate. We can use the List Component in ReactJS using the following approach. Prerequisites:NodeJS or NPMReactJSSteps to Create the React Application And In
2 min read
How to use XGrid Component in ReactJS ? An XGrid Component is a Commercial version of the MaterialUI Component. It provided more enhanced features over the community DataGrid Component like column resizing, column reordering, pagination over 100 rows, etc. A DataGrid Component helps in displaying the information in a grid-like format of r
3 min read
How to Pass JSON Values into React Components ? In React, you can pass JSON values into components using props. Props allow us to send data from a parent component to its child components. When rendering a component, you can pass the JSON values as props. These props can then be accessed within the componentâs function or class, allowing you to d
3 min read
How to use Filtering in DataGrid Component in ReactJS ? Using the DataGrid Component's filtering feature enables users to pinpoint specific records for a more targeted display. The ReactJS DataGrid Component arranges information in a grid layout, and implementing filtering follows a simple and user-friendly approach.Prerequisite:React JSMaterial UIApproa
2 min read
How to parse JSON Data into React Table Component ? In React parsing JSON Data into React Table Component is a common task to represent and structure data. We can render the JSON data into a table dynamically using the array map.Prerequisites:NPM & Node JSReact JSJSON parse()ApproachTo render JSON data in React Table we will be using the JavaScri
2 min read
How to use TreeView Component in ReactJS ? Explore the functionality of ReactJS's TreeView component, a powerful tool for seamlessly navigating and displaying hierarchical structures in web applications. This comprehensive guide delves into the integration process, empowering you to represent complex data relationships in an organized and us
2 min read
How to use map() to Create Lists in ReactJS ? The map function is generally used to render the list items dynamically in react. This function iterates over the array and each iteration returns the JSX for respective item. It helps to render and display the list efficiently. Prerequisites:React JSJavaScript Array.mapApproachTo create and render
2 min read