Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda