Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

313
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda