Skip to Content
ohooks

useWindowSize

Tracks the browser window's innerWidth and innerHeight on resize.

Loading preview...

The useWindowSize hook listens to window resize and orientationchange events (plus optional visualViewport resize events) and returns the current { width, height } dimensions.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useWindowSize.json

Usage

Examples

Responsive Canvas or Chart Sizing

Pass width and height to components that require explicit pixel dimensions.

Throttled Updates and SSR Initial Dimensions

Configure throttleMs to limit re-renders while dragging the window border, and initialWidth / initialHeight for deterministic SSR rendering.

API Reference

UseWindowSizeOptions

Configuration options for initial SSR dimensions, resize throttling, and visualViewport listening.

Property
Specification
initialWidth?
numberdefault:0
Fallback width used during SSR or when initializeWithValue is false
initialHeight?
numberdefault:0
Fallback height used during SSR or when initializeWithValue is false
initializeWithValue?
uniondefault:true
Read window.innerWidth/innerHeight synchronously on initial client render
throttleMs?
numberdefault:0
Optional throttle interval in milliseconds for resize updates

WindowSize

Window width and height dimensions returned by the hook.

Property
Specification
widthrequired
number
Viewport width (window.innerWidth).
heightrequired
number
Viewport height (window.innerHeight).