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

117
Visualizações
React List item logic handling

I'm curious to what's a more performant way of handling list logic. For example...

The Item component

export default function Item({data}) => {
  return <div>
    <h1>{data.name}
    <button> Do Something </button>
  </div>
}

The List component

export default function List({list}) => {
   return <div>
    {list.map(item) => <Item data={item} />}
   </div>
}

The Main component

export default function Main() {
  return <div>
    <List list={someList} />
  </div>
}

If i want to do something with the button in the Item component is it better to place the logic inside the Item component like this:

export default function Item({data}) => {
  const handleDoSomething = () => {
    logic goes here
  }

  return <div>
    <h1>{data.name}
    <button onClick={() => handleDoSomething()}> Do Something </button>
  </div>
}

Or should i propagate back the event to the List component and handle it there like this:

export default function Item({data, handleButtonClick}) => {

  return <div>
    <h1>{data.name}
    <button onClick={() => handleButtonClick()}> Do Something </button>
  </div>
}
export default function Main() {
  const handleDoSomething = () => {
    logic goes here
  }

  return <div>
    <List list={someList} handleButtonClick={() => handleDoSomething()} />
  </div>
}

What's the better to do this ? I want a better performance.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I guess that propagating back the event to the List component is better because the handler function will only be declared once, instead of once for every button that you render.

And btw, if you won't pass any custom argument to your handler function, you don't need to create an arrow function, you can just do:

<button onClick={handleButtonClick}> Do Something </button>
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