Created
December 18, 2021 08:39
-
-
Save 0xLDev/97380304521e1d7435134fb06d4e8b12 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useCallback, useEffect, useState } from 'react'; | |
import { useEventListener } from 'useEventListener'; | |
const INITIAL_SIZE = [0, 0]; | |
export const useWindowSize = () => { | |
const [size, setSize] = useState(INITIAL_SIZE); | |
useEffect(() => { | |
const { innerWidth, innerHeight } = window; | |
setSize([innerWidth, innerHeight]); | |
}, []); | |
useEventListener( | |
'resize', | |
useCallback((event) => { | |
const { innerWidth, innerHeight } = event.target; | |
setSize([innerWidth, innerHeight]); | |
}, []) | |
); | |
return size; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment