@wojtekmaj/react-hooks
Version:
A collection of React Hooks.
15 lines (14 loc) • 604 B
JavaScript
import { useCallback, useState } from 'react';
import useEventListener from './useEventListener.js';
const isBrowser = typeof window !== 'undefined';
/**
* Returns the interior height of the window in pixels.
*
* @returns {number | null} Height of the window in pixels
*/
export default function useWindowHeight() {
const [windowHeight, setWindowHeight] = useState(isBrowser ? window.innerHeight : null);
const getWindowHeight = useCallback(() => setWindowHeight(window.innerHeight), []);
useEventListener(isBrowser ? window : null, 'resize', getWindowHeight);
return windowHeight;
}