Skip to content

Instantly share code, notes, and snippets.

@jaysoo
Created October 15, 2019 19:28
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 jaysoo/12835078676974cf564226614555d993 to your computer and use it in GitHub Desktop.
Save jaysoo/12835078676974cf564226614555d993 to your computer and use it in GitHub Desktop.
import { useState, useEffect } from 'react';
export function useViewportSize() {
const getSize = () => {
return { width: window.innerWidth, height: window.innerHeight };
};
const [size, setSize] = useState(getSize);
useEffect(() => {
const handleResize = () => setSize(getSize());
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, [getSize]);
return size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment