From a24134f8525373f5707b6c48aa77d3f4aff70799 Mon Sep 17 00:00:00 2001 From: Mo Tarbin Date: Tue, 9 Jul 2024 17:39:16 -0400 Subject: Add useWindowWidth hook and HistoryCard component --- src/hooks/useWindowWidth.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/hooks/useWindowWidth.js (limited to 'src/hooks') diff --git a/src/hooks/useWindowWidth.js b/src/hooks/useWindowWidth.js new file mode 100644 index 0000000..92bf184 --- /dev/null +++ b/src/hooks/useWindowWidth.js @@ -0,0 +1,19 @@ +import { useEffect, useState } from 'react' +const useWindowWidth = () => { + const [windowWidth, setWindowWidth] = useState() + + useEffect(() => { + const handleResize = () => { + setWindowWidth(window.innerWidth) + } + + window.addEventListener('resize', handleResize) + + // Cleanup function to remove the event listener + return () => window.removeEventListener('resize', handleResize) + }, []) + + return windowWidth +} + +export default useWindowWidth -- cgit 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/hooks') 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