I have been trying to push an object into a state array and then into local storage so it can remain visible even after refreshing, everything works fine except final push doesent happen. In order for an object to stay saved in local i have to add it and one after it, and the one after dont get saved. Any help is much appreciated
function App() {
const [data, setData] = useState([{ name: "", thumbnail: { path: "" } }]);
const [team, setTeam] = useState(JSON.parse(localStorage.getItem("team")));
console.log(team);
useEffect(() => {
fetch(
"http://gateway.marvel.com/v1/public/characters?apikey="
)
.then((data) => data.json())
.then((data) => setData(data.data.results));
}, []);
const addToTeam = (hero) => {
!team ? setTeam([hero]) : setTeam([...team, hero]);
localStorage.setItem("team", JSON.stringify(team));
};