Good morning,
I need to save a state (which is a list of objects) in a localStorage, but the first value is always "[]", because I initialized the state that way.
const [favorites, setFavorites] = useState<IPodcast[]>([]);
On localStorage it looks like this:
I need the first value saved in localStorage to be the first value in my list. Below is the code responsible for saving the state in localStorage.
function adicionarFavorito(evento: React.FormEvent<HTMLFormElement>) {
evento.preventDefault();
podcastList.filter((p) => p.id === idPodcast)
.map((p) => (setFavorites((oldFavorites) => [...oldFavorites, { ...podcastList[p.num] }])));
localStorage.setItem("favorites", JSON.stringify(favorites));
}
Can someone help me?
Top comments (0)