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

129
Views
Cómo hacer que este componente esté en bucle en React (contando la cantidad de accesorios que recibe, hace algunos cálculos y tiene títulos estáticos)

Tengo los siguientes 3 elementos de la lista. Cada uno de ellos utiliza el mismo componente pero diferentes cálculos:

 <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} />

¿Cómo se puede convertir esto en 1 componente con el bucle?

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

0

No está claro en su código dónde se declaran tag , getCalculatedValue , color , etc.

Pero he aquí un enfoque básico. Mueva los elementos de su lista a una matriz y use un mapa para representar cada elemento de la lista.

 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!