• hooks

useResizer

import React, { useEffect } from 'react'

export default function useResizer() {
  const windowExists = typeof window !== 'undefined'
  const [isMobile, setIsMobile] = React.useState( windowExists && window.innerWidth < 575)

  useEffect(() => {
    function handleSizeChange() {
      return setIsMobile( windowExists && window.innerWidth < 575)
    }

    if(windowExists){
      window.addEventListener('resize', handleSizeChange)
    }

    return () => {
      if(windowExists){
        window.removeEventListener('resize', handleSizeChange)
      }
    }
  }, [isMobile, windowExists])

  return [isMobile]
}