Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

331
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!