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

210
Visualizações
React functional components how do they preserve the lexical environment of closures?

The following code, listens for any click and updates and shows a counter. The counter is stored in a state variable. The app listens for clicks and calls the increment function, which updates the counter.

What I don't understand is that

set_counter(counter + 1)

does not work. It updates the counter to 1 and then stops. However

set_counter(counter += 1)

which first assigns to counter directly and then calls the setter (which is a pattern that I am trying to avoid), works as expected.

Why is that? Does React not preserve the variables of the lexical environment where the functions are defined, unless you assign to them?


import React, {useEffect, useState} from 'react';

const Logs = () => {
        let [counter, set_counter] = useState(0);

        // Runs on every click
        const increment = () => {
                set_counter(counter += 1);
        };

        // Runs on first render
        useEffect(() => {
                window.addEventListener('click', increment, true);
                return () => window.removeEventListener('click', increment);
        }, []);

        return <div>{counter}</div>;
};

export default Logs;
about 4 years ago · Juan Pablo Isaza
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