Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda