Skip to content

Instantly share code, notes, and snippets.

@yansusanto
Created September 27, 2019 16:48
Show Gist options
  • Save yansusanto/1c9998ece3e18315f574a191c74c6add to your computer and use it in GitHub Desktop.
Save yansusanto/1c9998ece3e18315f574a191c74c6add to your computer and use it in GitHub Desktop.
import { useState, useLayoutEffect } from 'react';
export const useWindowWidth = () => {
const [windowWidth, setWindowWidth] = useState(window.innerWidth);
const handleWindowResize = () => {
setWindowWidth(window.innerWidth);
};
useLayoutEffect(() => {
window.addEventListener('resize', handleWindowResize);
return () => window.removeEventListener('resize', handleWindowResize);
}, []);
return windowWidth;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment