I created a <ThemeProvider/>
using useContext with React. Everything works fine in development environment. I'm able to pass colors down to components, and store the selected color theme in localStorage
using the useEffect hook (theme color can be selected by clicking on a button). Then the selected theme color can be retrieved from localStorage
after refresh.
I managed to deploy the website on Netlify, and what I've written in dev environment for local storage now doesn't behave the same way. I'm now unable to retrieve selected theme color from local storage.
Here is the a code snippet:
const [state, setState] = useState({
count: 0,
mainColor: themesArray[getTheme()].mainColor,
secondColor: themesArray[getTheme()].secondColor,
thirdColor: themesArray[getTheme()].thirdColor,
mainFilter: themesArray[getTheme()].mainFilter,
secondFilter: themesArray[getTheme()].secondFilter,
boxShadowRGB1: themesArray[getTheme()].boxShadowRGB1,
boxShadowRGB2: themesArray[getTheme()].boxShadowRGB2,
boxShadowRGB3: themesArray[getTheme()].boxShadowRGB3
});
useEffect(() => {
localStorage.setItem('theme', JSON.stringify(state.count));
}, [state.count]);
function getTheme() {
if (typeof window === 'object' || typeof window !== 'undefined') {
const savedTheme = JSON.parse(localStorage.getItem('theme'));
return savedTheme || 0;
} else return 0;
}
In getTheme() method, for the building process I had to wrap localStorage
in a condition because window object is unavailable during build. I wrote return 0 (for pointing the first theme in my themesArray) because otherwise it returns undefined and build fails. Problem is that, in production, by writing it that way I'm unable to retrieve selected theme after refresh. But in dev tools I can see that my theme item is stored with good values. I've got no knowledges about SSR so I feel a bit lost and don't know how to code this. Can someone help?