Skip to content

Instantly share code, notes, and snippets.

@nwellis
Created August 1, 2019 14:36
Show Gist options
  • Save nwellis/752c5d3316dda49546ad0582d84a5f3c to your computer and use it in GitHub Desktop.
Save nwellis/752c5d3316dda49546ad0582d84a5f3c to your computer and use it in GitHub Desktop.
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