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

329
Visualizações
In react re-rendering, how to prevent resetting of the scrollbar to topmost point of other non-changed components?

I have the following sample react code

const A = () => {
    const [value1, setValue1] = React.useState(true);
    const [value2, setValue2] = React.useState(true);

    return (
    <div>
        <div class="first-comp">
             <FirstComponent data1={value1} data2={value2} />
        </div>
        <div class="second-comp">
             <SecondComponent onChange={() => {setValue1(false); setValue2(false);}} />
        </div>
    </div>
    )
}

When the SecondComponent is calling onChange, then both "first-comp" and "second-comp" are being re-rendered on the screen.

Since on re-rendering of SecondComponent, my scroll is resetting (i.e. If I had scrolled the internal scrollbar down in SecondComponent, on rerendering the SecondComponent renders at the top and not at the point where I had last scrolled to)

Is there a way that, when onChange changes the stateVariables, the <SecondComponent /> does not get re-rendered or the scroll does not get disturbed in SecondComponent or somehow the scroll gets preserved?

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

0

The issue was that on changing the state using setValue, react was running the UI of the second component.

Using shouldComponentUpdate() as mentioned here https://www.geeksforgeeks.org/reactjs-shouldcomponentupdate-method/ or use React.memo to prevent this re-rendering

I configured to not update when the value1 & value2 changes

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