I am currently trying to add favorites to an exercise list with the following code
const addFavouriteExercise = (exercise) => {
const newFavouritesList = [...favourites, exercise];
setFavourites(newFavouritesList);
saveToLocalStorage(newFavouritesList);
};
It works locally, but when I deploy to netlify I get the error Uncaught TypeError: e is null, and it won't add it to local storage.
Any ideas?
saveToLocalStorage
const saveToLocalStorage = (items) => {
localStorage.setItem("favourites", JSON.stringify(items));
};
The bug ended up being in a useEffect that I had, where it was attempting to load items from localStorage but set the favourites to an empty object.
Bugs fixed, phew
The new useEffect looks like this:
useEffect(() => {
const exerciseFavourites = JSON.parse(localStorage.getItem("favourites"));
try {
let length = exerciseFavourites.length;
if (length > 0) {
setFavourites(exerciseFavourites);
}
} catch (e) {
console.error(e);
}
}, []);
You should find where the E is. And try using saveLocalStorage first and then setState to avoid rerender before setting it to localstorege