( initialWidth = Infinity, initialHeight = Infinity )
| 4 | // Reference: https://github.com/streamich/react-use/blob/master/src/useWindowSize.ts |
| 5 | |
| 6 | export const useWindowSize = ( |
| 7 | initialWidth = Infinity, |
| 8 | initialHeight = Infinity |
| 9 | ) => { |
| 10 | const [state, setState] = useRafState<{ width: number; height: number }>({ |
| 11 | width: isBrowser ? window.innerWidth : initialWidth, |
| 12 | height: isBrowser ? window.innerHeight : initialHeight, |
| 13 | }); |
| 14 | |
| 15 | useEffect((): (() => void) | void => { |
| 16 | if (isBrowser) { |
| 17 | const handler = () => { |
| 18 | setState({ |
| 19 | width: window.innerWidth, |
| 20 | height: window.innerHeight, |
| 21 | }); |
| 22 | }; |
| 23 | |
| 24 | window.addEventListener('resize', handler); |
| 25 | |
| 26 | return () => { |
| 27 | window.removeEventListener('resize', handler); |
| 28 | }; |
| 29 | } |
| 30 | }, []); |
| 31 | |
| 32 | return state; |
| 33 | }; |
no test coverage detected