Last active
January 23, 2020 07:15
-
-
Save Taump/557365495541e14afcd265028a26e167 to your computer and use it in GitHub Desktop.
hook windows resize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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