From 8da220e990b5ab15509848d4d7a57298eb6e808e Mon Sep 17 00:00:00 2001 From: Mo Tarbin Date: Tue, 9 Jul 2024 17:50:57 -0400 Subject: Refactor useWindowWidth hook to improve readability --- src/hooks/useWindowWidth.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/hooks/useWindowWidth.js b/src/hooks/useWindowWidth.js index 92bf184..fa79cc4 100644 --- a/src/hooks/useWindowWidth.js +++ b/src/hooks/useWindowWidth.js @@ -1,19 +1,14 @@ import { useEffect, useState } from 'react' -const useWindowWidth = () => { - const [windowWidth, setWindowWidth] = useState() +function useWindowWidth() { + const [width, setWidth] = useState(window.innerWidth) useEffect(() => { - const handleResize = () => { - setWindowWidth(window.innerWidth) - } - + const handleResize = () => setWidth(window.innerWidth) window.addEventListener('resize', handleResize) - - // Cleanup function to remove the event listener return () => window.removeEventListener('resize', handleResize) }, []) - return windowWidth + return width } export default useWindowWidth -- cgit