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

177
Visualizações
How to efficiently update array of components in React

I have a list of buttons, each button represents some data from my backend.

Now when I press two of these buttons I need to mark them and also all buttons in between red.

const Buttons = () => {
  const data = useState(getData())[0];
  const [pressedButtons, setPressedButtons] = useState([]);

  return ({data.map((buttonData, index) => 
    <Pressable
        style={{backgroundColor: pressedButtons.indexOf(index) > -1 ? "red" : "white"}}
        onPress={(index) => setPressedButtons(calculatePressedButtons(index))}>
      <Text>{buttonData.getText()}</Text>
    </Pressable>
  });
}

The problem with this approach is that when having many buttons like this (100+) whenever I need to update a single button the whole component is re-rendered which then results in a delayed feedback to pressing the button.

I've been looking for a solution, but the proposed ones (mostly with memo) don't seem to apply to my case. The parent still goes through all this map and recalculates props for each button.

How can I efficiently update the components so that the feedback to the button clicks is instant?

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

0

make each button a separate component and each one handles its own state internally. then pass a callback to your button component thet every time you pressed the button, aside from updating your button state, that callback will be called. after that you can use forwardRef and useHandleImperative api to expose your onclick functionality to the parent and using ref for every button component in the parent and in the callback you passed in the previous step specify which buttons shoud be updated afer a button has been clicked.

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you should make each button a separate component and each one handles its state internally

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