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

73
Visualizações
Array returning empty

I'm trying to add an object to an array using ReactJS on a button click. On the first click the array is returning empty in the console and I can't figure out why. It saves after the second click and second return of the array

const [favouriteBooks, setFavouriteBooks] = useState([]);
    
function addFavourite(event) {
    event.preventDefault();
    const favouritedBook = bookData[event.target.name];
    setFavouriteBooks((prev) => [...prev, favouritedBook]);
    console.log(favouriteBooks);
}

in after the first click the console outputs an empty array. I want to save the array as a JSON string in the local browser storage and when I tried that I was having the same issue.

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