I have some problem with window global object on build project in gatsby. I use custom media hook for change login in some resolutions. It's work fine when project running. But when i try npm run build for build project Webpack gives error : WebpackError: TypeError: window.matchMedia is not a function
const useMedia = (query) => {
const windowGlobal = typeof window !== 'undefined' && window
const [matches, setMatches] = useState(windowGlobal.matchMedia(query).matches)
useEffect(() => {
const media = windowGlobal.matchMedia(query)
if (media.matches !== matches) setMatches(media.matches)
const listener = () => setMatches(media.matches)
media.addEventListener('change', listener)
return () => media.removeEventListener('change', listener)
}, [matches, query])
return matches
}
const match = useMedia('(max-width: 575px)')
const changeLabel = match ? 'Cancel' : 'Add Now'