I have my theme stored in a stateful function that defaults to false. I then passed a onclick function to toggle between false and true. I want that stored in localstorage and to persist on refresh. But i can't access local storage in nextJS as the initial value.
const [dark, setDark] = useState(false);
useEffect(() => {
localStorage.setItem("theme", JSON.stringify(dark));
}, [dark]);
useEffect(() => {
const theme = JSON.parse(localStorage.getItem("theme"));
if (theme) {
setDark(theme);
}
}, []);
function setTheme() {
setDark(!dark);
}