So I have a problem with state and localStorage. To be exact, whenever I nest the objects inside of the themes.dark and themes.light the results in dynamic codeblock differ after refresh even though the localStorage data is in fact correct.
import { useState, useEffect } from "react";
import store from "store2";
import "./styles.css";
const themes = {
dark: {
theme: "dark"
},
light: {
theme: "light"
}
};
export default function App() {
const defaultDarkTheme = window.matchMedia("(prefers-color-scheme:dark)")
.matches
? themes.dark
: themes.light;
const [theme, setTheme] = useState(store.get("theme") || defaultDarkTheme);
useEffect(() => {
store.set("theme", theme);
}, [theme]);
return (
<>
<div
className="App"
onClick={() =>
theme === themes.dark ? setTheme(themes.light) : setTheme(themes.dark)
}
>
{JSON.stringify(theme)}
</div>
{theme === themes.dark
? JSON.stringify(themes.dark)
: JSON.stringify(themes.light)}
</>
);
}
Before refresh :
After refresh :
It's pretty problematic since after refresh instead of generating the content for dark mode it generates the content for the light one.

