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

158
Visualizações
React setState hook's previous param object gives inconsistent values

I'm using a useEffect hook to trigger a setState update. I'm getting some strange and inconsistent behaviour from the previous param:

    useEffect(() => {
        setCurrentPicturesObject((existing) => {
            const clone = {...existing}
            console.log({
                existing,
                existingdotNocolor: existing.nocolor,
                selectedColorState,
                selectedColorArray: existing["nocolor"],
                clone
            });
            return existing
        });
    }, [selectedColorState]);

So you'd expect that the clone of the object would return an object with the same keys and values, right? Not here:

screenshot of console log

Somehow existing goes from being an object with a nocolor prop with an array of two strings, to switching to an array with one string. Similarly when I try to access the nocolor prop it only returns an array with one string.

I can't understand. existing changes as soon as I try to access it with anything other than simply console logging it directly?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Not entirely sure on the details, but essentially it seems that the console is logging a live view of the object, which is a static string. More info in this answer.

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