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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read