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
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 falseFull Signature
initialHeight?numberdefault:0
Fallback height used during SSR or when
initializeWithValue is falseFull Signature
initializeWithValue?uniondefault:true
Read
window.innerWidth/innerHeight synchronously on initial client renderFull Signature
throttleMs?numberdefault:0
Optional throttle interval in milliseconds for resize updates
Full Signature
WindowSize
Window width and height dimensions returned by the hook.
Property
Specification
widthrequirednumber
Viewport width (
window.innerWidth).heightrequirednumber
Viewport height (
window.innerHeight).