so I'm getting an error saying cannot destructure property 'currentTheme' object as it is undefined
//Layout
const {currentTheme, cursorStyles} = useGlobalStateContext()
const dispatch = useGlobalDispatchContext()
//GlobalContext
const globalReducer = (state, action) => {
switch(action.type) {
case "TOGGLE_THEME": {
return {
...state,
currentTheme: action.theme,
}
}
case "CURSOR_TYPE": {
return {
...state,
cursorType: action.cursorType,
}
}
default : {
throw new Error(unhandled action type: ${action.type}
)
}
}
}
//Provider
export const GlobalProvider = ({children}) => {
const [state, dispatch] = useReducer(globalReducer, {
currentTheme: window.localStorage.getItem("theme") === null ? "dark" : window.localStorage.getItem("theme"),
cursorType: false,
cursorStyles: ["pointer", "hovered", "nav-open"]
})
Top comments (5)
Hello! This is going to be late but... here we go
This is happening because gatsby needs to know what are you doing with useContext in a ssr mode. In your case, you need to have the same gatsby-browser.js code in your gatsby-ssr.js code.
Also, the window object only works in the browser, so you can make this tricky code to avoid the error
That's it! I was facing the same error yesterday by following the Awwwards rebuilt video by Wrong Akram hahaha.
The same here, Akram doesn't take it to deployment, maybe there is a link?
Dude... I was stuck with this issue for a few hours.
Thank you for solving it for me!
This helped me a lot too. I fixed the issues by copying the gatsby-browser.js to gatsby-ssr.js and also checking for undefined for localstorage. Thanks a lot!!!!
pls who will help me with this. I am new to gatsby and I want to use this few lines of codes you used as well. but am still getting error. The solution didn't work for me. How did you manage to run npm run build without getting those error? pls i need help.