Skip to content

Instantly share code, notes, and snippets.

@joepuzzo
Created June 20, 2022 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joepuzzo/9980f2d9490a0b5568f59b45c57cf89d to your computer and use it in GitHub Desktop.
Save joepuzzo/9980f2d9490a0b5568f59b45c57cf89d to your computer and use it in GitHub Desktop.
React hook to get media size from window using other gist
import { useEffect, useState } from 'react';
import * as media from '../utils/media';
const useMedia = () => {
const [isDesktopUp, setIsDesktopUp] = useState(media.isDesktopUp());
useEffect(() => {
const handleResize = () => {
setIsDesktopUp(media.isDesktopUp());
};
// For resizing header
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener(handleResize);
};
}, []);
return { isDesktopUp };
};
export default useMedia;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment