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

252
Views
React: How to create a Favorites array using hooks?

I would like to show a Favorite Art Pieces list using hooks, the array is actually created in favs, but I don't know how to connect to favorites using setFavorites . Here is my code simplified:

import artData from "../data/data.json";

function App() {
  const [favorites, setFavorites] = useState([""]);
  let favs = [];

  const updateFav = (ev) => {
    const selectedArt = parseInt(ev.currentTarget.id);
    const objectClicked = artData.find((art) => {
      return art.id === selectedArt;
    });
   const favFounded = favs.findIndex((fav) => {
   return fav.id === selectedArt;
    });
    if (favFounded === -1) {
      favs.push(objectClicked);
    } else {
      favs.splice(favFounded, 1);
    }
    console.log(favs);
    //setFavorites(favs);
  };
return (
    <>
      <button className="fav" id={art.id} onClick={updateFav}>
<h2 className="fav__title"> here are your favorites list..</h2>
<ul className="fav__list">
  <li className="fav__list--element">
//the idea here is to map my favorites list once I can fix the problem
  </li>
</ul>
    </>
  );

Thanks,

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!