Skip to content

Instantly share code, notes, and snippets.

@Taump
Last active January 23, 2020 07:15
Show Gist options
  • Save Taump/557365495541e14afcd265028a26e167 to your computer and use it in GitHub Desktop.
Save Taump/557365495541e14afcd265028a26e167 to your computer and use it in GitHub Desktop.
hook windows resize
import { useLayoutEffect, useState } from "react";
export const useWindowSize = () => {
const [size, setSize] = useState([0, 0]);
useLayoutEffect(() => {
function updateSize() {
setSize([window.innerWidth, window.innerHeight]);
}
window.addEventListener("resize", updateSize);
updateSize();
return () => window.removeEventListener("resize", updateSize);
}, []);
return size;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment