Created
August 1, 2019 14:36
-
-
Save nwellis/752c5d3316dda49546ad0582d84a5f3c 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 { useMemo, useEffect, useState } from 'react'; | |
import { useDebounce } from 'use-debounce'; | |
export default function(debounceWaitMs = 500) { | |
const [curWindowHeightPx, setWindowHeight] = useState(window.innerHeight); | |
const [windowHeightPx] = useDebounce(curWindowHeightPx, debounceWaitMs, { | |
leading: true | |
}); | |
useEffect(() => { | |
const handleWindowResize = () => { | |
setWindowHeight(window.innerHeight); | |
}; | |
window.addEventListener('resize', handleWindowResize); | |
return function cleanup() { | |
window.removeEventListener('resize', handleWindowResize); | |
}; | |
}); | |
return useMemo( | |
() => windowHeightPx, | |
[windowHeightPx] | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment