Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/react-hooks. A collection of React Hooks.
hooks react react-hooks
Something went wrong. Try again.
820 B · 27 lines
TypeScript
at main
12345678910111213141516171819202122232425262728import { useCallback, useState } from 'react';
import useEventListener from './useEventListener.js';
const isBrowser = typeof window !== 'undefined';
function getWindowSize(): { width: number; height: number } { return { width: window.innerWidth, height: window.innerHeight, };}
/** * Returns the interior width and height of the window in pixels. * * @returns {{ width: number; height: number } | null} Width and height of the window in pixels */export default function useWindowSize(): { width: number; height: number } | null { const [windowSize, setWindowSize] = useState(isBrowser ? getWindowSize() : null);
const updateWindowSize = useCallback(() => setWindowSize(getWindowSize()), []);
useEventListener(isBrowser ? window : null, 'resize', updateWindowSize);
return windowSize;}