enter image description hereI want to create a button that saves page data in a cookie. I do a spread (newFav) of my useState fav then I stringify the data in a constant that I push in my newFav array, I place the newFaw array in my useState fav. I do a check that fav.length >0 then I do a cookies.set but it doesn’t create: could you tell me where my error is, please? I’m tearing my hair out!
here my useState in app.jsx:
const [fav, setFav] = useState([Cookies.get('gamePad')] || null);
my handleClick: const handleFav = () => {
try {
const newFav = [...fav];
console.log('newFav before push:', newFav);
//au click, je stringify l'object card
const newCard = JSON.stringify(data);
console.log('newCard:', newCard);
// je l'ajoute au tableau clone de fav
newFav.push(newFav + newCard);
console.log('newFav after push:', newFav);
console.log('typeof newFav after push:', Array.isArray(newFav));
//je l'ajoute à mon useState fav)
setFav(newFav);
console.log('fav after setFav:', fav)
//je crée un cookie avec la valeur de fav
console.log('fav.length:', fav.length);
Cookies.set('favorites', fav, { expires: 15, secure: true });
} catch (error) {
console.log('error:', error);
}
}
I have the console.log with the cookie value, but the value is not saved when I click on the inspector.
jerome bobin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.