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

310
Views
Does functional component in react trigger a re render?

In react functional component, the docs specify that updating state to a value identical to the previous state, will not cause a re-render (https://reactjs.org/docs/hooks-reference.html#bailing-out-of-a-state-update)

If you update a State Hook to the same value as the current state, React will bail out without rendering the children or firing effects. (React uses the Object.is comparison algorithm.)

In this piece of code here -

const App = () => {
    const [resourceType, setResourceType] = useState('posts');
    
    console.log('abc');
    
    return (
        <>
            <button onClick={() => setResourceType('posts')}>posts</button>
            <button onClick={() => setResourceType('users')}>users</button>
            <button onClick={() => setResourceType('comments')}>comments</button>
            <h2>{resourceType}</h2>
        </>
    );
}

When I go on clicking different buttons I can see abc for every different button click in the console which is correct as the resourceType is getting changed and the page re-renders. However, when I click on the same button for the second time I can still see the abc getting logged in the console which is weird as the docs mention that setting the same value as the current state won't cause the re-render.

And to my amazement when I click the same button thrice or even more times the abc isn't logged anymore as if someone told him that it's not his turn. Can anyone help me with that behaviour of react?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

An explanation to why you might get a render when setting the state to exact same value as the previous one is on the following comment from a Facebook developer.

The issue I linked explains a similar problem where clicking on a button that sets the state to the same value results in a re-render on the first and second click, and not on the third one. The reason for the render to happen twice is that:

This is a known quirk due to the implementation details of concurrency in React. We don't cheaply know which of two versions is currently committed. When this ambiguity happens we have to over render once and after that we know that both versions are the same and it doesn't matter.

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!