Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

72
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!