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

317
Visualizações
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 Respostas
Responde à pergunta

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 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