how can I work around local Storage is not defined error in next.js. I am quite new to next.js and react. I know that we require to do some form of mounting before checking for local storage data but I cant figure out a way to do it. I am trying to save into local storage so the data doesn't disappear when the page is refreshed
export const GlobalContext = React.createContext();
export const GlobalProvider = (props) => {
const initialState = {
watchlist: localStorage.getItem("watchlist")
? JSON.parse(localStorage.getItem("watchlist"))
: [],
};
const [state, dispatch] = React.useReducer(AppReducer, initialState);
React.useEffect(() => {
localStorage.setItem("watchlist", JSON.stringify(state.watchlist));
}, [state])
const addtoWatch = coin => {
dispatch({ type: "ADD_COIN_TO_WATCHLIST", payload: coin })
}
return (
<GlobalContext.Provider
value={{
watchlist: state.watchlist,
addtoWatch,
}}
>
{props.children}
</GlobalContext.Provider>
);
};