use-window-width.ts
1 import { useState, useEffect } from 'react'; 2 3 const useWindowWidth = () => { 4 const [windowWidth, setWindowWidth] = useState(window.innerWidth); 5 6 useEffect(() => { 7 function handleResize() { 8 setWindowWidth(window.innerWidth); 9 } 10 11 window.addEventListener('resize', handleResize); 12 return () => window.removeEventListener('resize', handleResize); 13 }, []); 14 15 return windowWidth; 16 }; 17 18 export default useWindowWidth;