Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

75
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda