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

90
Visualizações
Why does calling a state change multiple times not cause multiple re-renders?
interface params{
  texts: string[];
  handlePropertyTerm: (value:string) => void;
}

let SortOptions = React.memo(({texts, handlePropertyTerm}:params) => (
    <>
      {texts.map((text) => (
        <button key={text} defaultValue={text} onClick={() => handlePropertyTerm(text)}>{text}</button>
      ))}
    </>
));
function App(){
    let [sortProperty, setPropertyTerm] = React.useState("");
    let sortPropertyTerm = React.useCallback((value) => {
        console.log("Callback value:", value);
        setPropertyTerm(value);
    }, []);
    console.log("App");


    return (
    <>
    <SortOptions texts={["hi", "bye"]} handlePropertyTerm={sortPropertyTerm}></SortOptions>
    </>
    );
}

The code i described above creates two buttons one called hi and another called bye each with a callback which causes the sortProperty to contain either "hi" or "bye" depending on the button clicked. After App first loads and i click the hi button, it will log Callback value = hi and App. I believe this is because there was a state change in App causing a re-render. When I click hi the second time the same statements are logged. When I click hi the third time, only the Callback value = hi is logged.

Please can someone explain what is causing App to be re-rendered the second time I click hi but not the third time?

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