Today, we focused on rendering lists in React using the powerful .map()
method. This approach makes it simple to dynamically display collections of data in your components.
We created two components, UserList
and ProductList
, to render dynamic lists of users and products, respectively. These components utilize the .map()
method to iterate over arrays and display content dynamically.
-
File:
UserList.jsx
-
Description:
A functional component that displays a list of users, with each user'sname
andage
. -
Code:
import React from "react"; const UserList = () => { const users = [ { id: 1, name: "Alice", age: 25 }, { id: 2, name: "Bob", age: 30 }, { id: 3, name: "Charlie", age: 22 }, ]; return ( <div> <h2>User List</h2> {users.map((user) => ( <div key={user.id}> <p>Name: {user.name}</p> <p>Age: {user.age}</p> </div> ))} </div> ); }; export default UserList;
-
File:
ProductList.jsx
-
Description:
A functional component that displays a list of products, with each product'sname
andprice
. -
Code:
import React from "react"; const ProductList = () => { const products = [ { id: 1, name: "Phone", price: "$699" }, { id: 2, name: "Laptop", price: "$1200" }, { id: 3, name: "Headphones", price: "$199" }, ]; return ( <div> <h2>Product List</h2> {products.map((product) => ( <div key={product.id}> <p>Name: {product.name}</p> <p>Price: {product.price}</p> </div> ))} </div> ); }; export default ProductList;
-
File:
App.jsx
-
Description:
Import theUserList
andProductList
components and render them. -
Code:
import React from "react"; import UserList from "./UserList"; import ProductList from "./ProductList"; function App() { return ( <div> <UserList /> <ProductList /> </div> ); } export default App;
The .map()
method is used to iterate over arrays and return JSX elements for each item.
Example:
const items = ["Apple", "Banana", "Cherry"];
items.map((item, index) => <p key={index}>{item}</p>);
Keys help React identify which items have changed, are added, or removed, improving rendering performance.
Example:
users.map((user) => <div key={user.id}>{user.name}</div>);
We learned how to break down UI into smaller, reusable components and compose them in a parent component.
- Dynamic Rendering: Learned how to display dynamic data in React using
.map()
. - Using Keys: Understood the importance of unique keys in lists for efficient rendering.
- Reusability: Practiced creating reusable components for structured and maintainable code.
Happy Coding! ๐