I've tried everything to add local storage to persist theme. Any ideas on how to implement with the existing code? I get a mix of conflicting themes upon first rendering. I tried implementing local storage when setting theme.
const setTheme = type => {
localStorage.setItem("theme", setState({ ...state, theme: type === "dark" ? theme.light : theme.dark })
}
const initState = {
theme: localStorage.getItem("theme"),
setTheme: setTheme,
}
const [state, setState] = useState(initState)
ThemeProvider.js
export const ThemeContext = React.createContext({
theme: {
},
setTheme: () => {},
})
export const ThemeContextProvider = props => {
const theme = {
light: {
},
dark: {
},
}
const setTheme = type => {
setState({ ...state, theme: type === "dark" ? theme.light : theme.dark })
}
const initState = {
theme: theme.light,
setTheme: setTheme,
}
const [state, setState] = useState(initState)
return (
<ThemeContext.Provider value={state}>
{props.children}
</ThemeContext.Provider>
)
}