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 state hook Object not updating, getting [Object object]

In my project I am having trouble setting a state variable with a javascript object, I can see in my console logs that the object is being instansiated correctly with the right values, but I cannot figure out why it is not properly setting the state hook.

Any help would be appreciate!!

const [accountInfo, setAccountInfo] = useState({})

const delay = ms => new Promise(res => setTimeout(res, ms));

const onLogin = async (account) => {

    await(delay(2000));

    console.log(account); // I am getting the proper output of the object here

    if(account.account_id != -1){

      setAccountInfo(account);

      await delay(4000); //Just for debugging
      console.log("acc " + accountInfo); // This outputs [Object object]
      setLoggedIn(true);
    }else{
      alert('Account does not exist');
    }
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are trying to concatenate string with object. Either convert object to string with JSON.stringify or use object.property.

console.log("Object" + JSON.stringify(object_to_print));

you are trying to console.log a state variable. React is not guaranteeing the state change immediately. you can access state change in useEffect hook

useEffect(() => { 
      console.log('state changed', your-state-variable) 
      // write your callback function here
 }, [your-state-variable]);
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