'Uncaught TypeError: e is null
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));
};
Solution 1:[1]
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);
}
}, []);
Solution 2:[2]
You should find where the E is. And try using saveLocalStorage first and then setState to avoid rerender before setting it to localstorege
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ahmad Shahbalayev |
