简体   繁体   English

window.matchMedia 不是函数

[英]window.matchMedia is not a function

I have some problem with window global object on build project in gatsby.我在 gatsby 的构建项目中遇到了window全局对象的一些问题。 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但是当我尝试npm run build for build project Webpack 给出错误: 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'

Try:尝试:

 const useMedia = (query) => {
    const [matches, setMatches] = useState(typeof window !=='undefined' && window.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'

You were fixing the value of windowMedia (same as isBrowser ) with a const and the state was trying to set a value of undefined at the time that the window was not set, you can simplify by setting the state only when the window is defined.你是固定的值windowMedia (同isBrowser用) const和国家试图设置的值undefined在该时间window还没有设置,你可以通过设置,只有当国家简化window定义。

You're doing it wrong.你这样做是错的。 You're setting the const windowGlobal to be true/false and then use it in useState(windowGlobal...) which means windowGlobal is a bool and not a function.您将 const windowGlobal 设置为 true/false,然后在useState(windowGlobal...)使用它,这意味着 windowGlobal 是一个 bool 而不是函数。 Use window directly like my example below:像我下面的例子一样直接使用 window :

const windowGlobal = typeof window !== 'undefined' && window
const [matches, setMatches] = useState(windowGlobal.matchMedia(query).matches)

Id go with making an if window is set, then execute the code.我去设置一个 if 窗口,然后执行代码。

const useMedia = (query) => {
  const [matches, setMatches] = useState(windowGlobal.matchMedia(query).matches)
  
  const isBrowser = () => typeof window !== 'undefined' && window;
  
  useEffect(() => {
    if (isBrowser()) {
      const media = window.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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM