diff --git a/coding_interviews/frontend/react/useWindowSize.js b/coding_interviews/frontend/react/useWindowSize.js new file mode 100644 index 0000000..0d84731 --- /dev/null +++ b/coding_interviews/frontend/react/useWindowSize.js @@ -0,0 +1,21 @@ +import { useState, useEffect } from 'react'; + +function useWindowSize() { + const [height, setHeight] = useState(window.innerHeight); + const [width, setWidth] = useState(window.innerWidth); + + useEffect(() => { + const handleResize = () => { + setHeight(window.innerHeight); + setWidth(window.innerWidth); + }; + + window.addEventListener('resize', handleResize); + + return () => { + window.removeEventListener('resize', handleResize); + }; + }, []); + + return { width, height }; +}