I have the following problem with a (hopefully) basic function of useState which I do not get yet. I wanna get a todo list with all the previously written tasks. I try to solve it in two ways:
First approach:
const [ favoritedList, setFavoritedList ] = useState([]);
const handleFavoritedVideo = (favoritedElement) => {
setFavoritedList((previousFavorited) => {
return [ favoritedElement, ...previousFavorited ];
});
};
everything is working fine.
But when I try to do something like this:
const [ favoritedList, setFavoritedList ] = useState({favorited:[]});
const handleFavoritedVideo = (favoritedElement) => {
setFavoritedList((previousFavorited) => {
return [ favoritedElement, ...previousFavorited ];
});
};
I get the following error:
TypeError: previousFavorited is not iterable
Why is this and how to fix it?