PS- This post is the prime example on how we forget the basic things ...man i am sure feeling dumb XD
I am new Here so stack overflow dosnt let me embed the pics..
I am creating an Ebook management system where i wanted to make favorite book list when i click "add to fav" button on every book
this function is called when i press the "add to fav" button where favList is an empty array state
const [favList, setfavList] = useState([]);
//BookList is an array of books which are fetched from google books API
const addFav = () => {
return setfavList(
...favList,
BookList.find((t) => id == t.id)
);
};
But whenever i click multiple books an error occurs The Error
Here is the declaration of my states State Declaration
Are you using hooks, show the code where you declaring the favlist
Try this:
const addFav = () => {
if(favList.length)
return setfavList(
...favList,
BookList.find((t) => id == t.id)
);
else
return setfavList(
BookList.find((t) => id == t.id)
);
};
Never mind i found what i had to do
const addFav = () => {
return setfavList([
...favList,
BookList.find((t) => id == t.id)]
);
};
we all were just missing the square braces yeah kinda lame but thanks for all the help