Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
How to make this component looped in React (by counting the fat thact it receives props and does some calculation and has static titles)

I have the following 3 list items. Each of them uses the same component but different calculations:

<ListItem title={'Product 1'} tag={tag} calculatedVal={getCalculatedVal(props.data.price1, props.data.price2)}
isMore={props.data.price1 >= props.data.price2}
handleClick={open}
/>
<Underline />
<ListItem title={'Product 2'} tag={tag} calculatedVal={getCalculatedVal(props.data.price1, props.data.price3)}
isMore={props.data.price1 >= props.data.price3}
handleClick={open}
/>
<Underline/>
<ListItem title={'Product 3'} tag={tag} calculatedVal={getCalculatedVal(props.data.price1, props.data.price4)}
isMore={props.data.price1 >= props.data.price4}
handleClick={open}
/>

How can be this converted into 1 component with the loop?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It's not clear from your code where tag, getCalculatedValue, color, etc. are declared.

But here's a basic approach. Move your list item props into an array and use a map to render each list item.

import ListItem from "./somewhere/ListItem";
import Underline from "./somewhere/Underline";
import { getCalculatedVal } from "./somewhere/getCalculatedVal";

const PRICES = ["price2", "price3", "price4"];

export const MyComponent = ({ data, ...props }) => {
  const listItems = PRICES.map((price) => {
    return {
      calculatedVal: getCalculatedVal(data.price1, data[price]),
      isMore: data.price1 >= data[price],
      key: price,
    };
  });

  return listItems.map((listItemProps) => {
    return (
      <>
        <Underline color={props.color} />
        <ListItem
          title={"Product 1"}
          tag={props.tag}
          handleClick={props.open}
          {...listItemProps}
        />
      </>
    );
  });
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!