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.
606 B · 20 lines
TypeScript
at main
123456789101112131415161718192021import { useCallback, useState } from 'react';
import useEventListener from './useEventListener.js';
const isBrowser = typeof window !== 'undefined';
/** * Returns the interior width of the window in pixels. * * @returns {number | null} Width of the window in pixels */export default function useWindowWidth(): number | null { const [windowWidth, setWindowWidth] = useState(isBrowser ? window.innerWidth : null);
const getWindowWidth = useCallback(() => setWindowWidth(window.innerWidth), []);
useEventListener(isBrowser ? window : null, 'resize', getWindowWidth);
return windowWidth;}