Skip to main content

useWindowSize

function useWindowSize(serverSize?: WindowSize): WindowSize;

interface WindowSize {
readonly width: number;
readonly height: number;
}

Returns the window's innerWidth and innerHeight, which include any scrollbar, and re-renders when the window is resized. It returns the same object while the size is unchanged.

During server rendering and on the first render while hydrating it returns serverSize (0 × 0 by default), then the real size, so hydration never mismatches. To switch layouts at a breakpoint, prefer useMediaQuery, which only re-renders when the breakpoint is crossed.

Live demo​

Landscape

0 × 0 px

Resize your browser window to see the size update.

Example​

import { useWindowSize } from "react-honey-hooks";

function Confetti() {
const { width, height } = useWindowSize();

return <canvas width={width} height={height} />;
}