How to Minimize Code Duplication in React (For Loop Problem)

I want to create multiple Profile Cards in my React app, but I’m currently using repetitive code, which doesn’t seem efficient. I tried using a for loop, but it didn’t work as expected. How can I use a for loop or any other method to make my code more concise and efficient?

Current Code:

App.jsx:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import React from "react";
import Contacts from "../contacts";
import Card from "./Card.jsx";
function App(props) {
return (
<div>
<h1 className="heading">My Contacts</h1>
<Card
name={Contacts[0].name}
imgURL={Contacts[0].imgURL}
phone={Contacts[0].phone}
email={Contacts[0].email}
/>
<Card
name={Contacts[1].name}
imgURL={Contacts[1].imgURL}
phone={Contacts[1].phone}
email={Contacts[1].email}
/>
<Card
name={Contacts[2].name}
imgURL={Contacts[2].imgURL}
phone={Contacts[2].phone}
email={Contacts[2].email}
/>
</div>
);
}
export default App;
</code>
<code>import React from "react"; import Contacts from "../contacts"; import Card from "./Card.jsx"; function App(props) { return ( <div> <h1 className="heading">My Contacts</h1> <Card name={Contacts[0].name} imgURL={Contacts[0].imgURL} phone={Contacts[0].phone} email={Contacts[0].email} /> <Card name={Contacts[1].name} imgURL={Contacts[1].imgURL} phone={Contacts[1].phone} email={Contacts[1].email} /> <Card name={Contacts[2].name} imgURL={Contacts[2].imgURL} phone={Contacts[2].phone} email={Contacts[2].email} /> </div> ); } export default App; </code>
import React from "react";
import Contacts from "../contacts";
import Card from "./Card.jsx";

function App(props) {
  return (
    <div>
      <h1 className="heading">My Contacts</h1>

      <Card
        name={Contacts[0].name}
        imgURL={Contacts[0].imgURL}
        phone={Contacts[0].phone}
        email={Contacts[0].email}
      />

      <Card
        name={Contacts[1].name}
        imgURL={Contacts[1].imgURL}
        phone={Contacts[1].phone}
        email={Contacts[1].email}
      />

      <Card
        name={Contacts[2].name}
        imgURL={Contacts[2].imgURL}
        phone={Contacts[2].phone}
        email={Contacts[2].email}
      />
    </div>
  );
}

export default App;

Contact Data (contacts.js):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const contacts = [
{
name: "Beyonce",
imgURL:
"https://blackhistorywall.files.wordpress.com/2010/02/picture-device-independent-bitmap-119.jpg",
phone: "+123 456 789",
email: "[email protected]",
},
{
name: "Jack Bauer",
imgURL:
"https://pbs.twimg.com/profile_images/625247595825246208/X3XLea04_400x400.jpg",
phone: "+987 654 321",
email: "[email protected]",
},
{
name: "Chuck Norris",
imgURL:
"https://i.pinimg.com/originals/e3/94/47/e39447de921955826b1e498ccf9a39af.png",
phone: "+918 372 574",
email: "[email protected]",
},
];
export default contacts;
</code>
<code>const contacts = [ { name: "Beyonce", imgURL: "https://blackhistorywall.files.wordpress.com/2010/02/picture-device-independent-bitmap-119.jpg", phone: "+123 456 789", email: "[email protected]", }, { name: "Jack Bauer", imgURL: "https://pbs.twimg.com/profile_images/625247595825246208/X3XLea04_400x400.jpg", phone: "+987 654 321", email: "[email protected]", }, { name: "Chuck Norris", imgURL: "https://i.pinimg.com/originals/e3/94/47/e39447de921955826b1e498ccf9a39af.png", phone: "+918 372 574", email: "[email protected]", }, ]; export default contacts; </code>
const contacts = [
  {
    name: "Beyonce",
    imgURL:
      "https://blackhistorywall.files.wordpress.com/2010/02/picture-device-independent-bitmap-119.jpg",
    phone: "+123 456 789",
    email: "[email protected]",
  },
  {
    name: "Jack Bauer",
    imgURL:
      "https://pbs.twimg.com/profile_images/625247595825246208/X3XLea04_400x400.jpg",
    phone: "+987 654 321",
    email: "[email protected]",
  },
  {
    name: "Chuck Norris",
    imgURL:
      "https://i.pinimg.com/originals/e3/94/47/e39447de921955826b1e498ccf9a39af.png",
    phone: "+918 372 574",
    email: "[email protected]",
  },
];

export default contacts;

Card.jsx

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import React from "react";
import Contact from "../contacts";
function Card(props) {
return (
<div>
<div className="card">
<div className="top">
<h2 className="name">{props.name}</h2>
<img src={props.imgURL} alt="avatar_img" className="circle-img" />
</div>
<div className="bottom">
<p>{props.phone}</p>
<p>{props.email}</p>
</div>
</div>
</div>
);
}
export default Card;
</code>
<code>import React from "react"; import Contact from "../contacts"; function Card(props) { return ( <div> <div className="card"> <div className="top"> <h2 className="name">{props.name}</h2> <img src={props.imgURL} alt="avatar_img" className="circle-img" /> </div> <div className="bottom"> <p>{props.phone}</p> <p>{props.email}</p> </div> </div> </div> ); } export default Card; </code>
import React from "react";
import Contact from "../contacts";

function Card(props) {
  return (
    <div>
      <div className="card">
        <div className="top">
          <h2 className="name">{props.name}</h2>
          <img src={props.imgURL} alt="avatar_img" className="circle-img" />
        </div>
        <div className="bottom">
          <p>{props.phone}</p>
          <p>{props.email}</p>
        </div>
      </div>
    </div>
  );
}

export default Card;

I want to reduce the repetition in the code above. How can I achieve this? Any suggestions for using a loop or another method to render the components more efficiently?

You can also check out the entire code here. 🙂

I tried using a for loop to dynamically create the components, but I couldn’t get it to work within the JSX. I expected the loop to iterate through my contacts array and render each contact as a separate component. However, my approach didn’t work as JSX doesn’t directly support for loops.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function App(props) {
return (
<div>
<h1 className="heading">My Contacts</h1>
{
for (let i = 0; i < Contacts.length; i++) {
<Card
name={Contacts[i].name}
imgURL={Contacts[i].imgURL}
phone={Contacts[i].phone}
email={Contacts[i].email}
/>
}
}
</div>
);
}
</code>
<code>function App(props) { return ( <div> <h1 className="heading">My Contacts</h1> { for (let i = 0; i < Contacts.length; i++) { <Card name={Contacts[i].name} imgURL={Contacts[i].imgURL} phone={Contacts[i].phone} email={Contacts[i].email} /> } } </div> ); } </code>
function App(props) {
  return (
    <div>
      <h1 className="heading">My Contacts</h1>
      {
        for (let i = 0; i < Contacts.length; i++) {
          <Card
            name={Contacts[i].name}
            imgURL={Contacts[i].imgURL}
            phone={Contacts[i].phone}
            email={Contacts[i].email}
          />
        }
      }
    </div>
  );
}

This code resulted in a syntax error because JSX does not support for loops directly. I need help finding a correct and efficient way to render multiple components without repetitive code.

New contributor

sulove gairhe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật