useWindowDimensions is a React hook that returns the current dimensions of the application window. It automatically updates when the window is resized, making it ideal for creating responsive layouts.
Signature
Returns
An object containing:width- The width of the window in density-independent pixels (dp)height- The height of the window in density-independent pixels (dp)scale- The pixel density of the device (e.g., 2 for @2x, 3 for @3x)fontScale- The font scale factor set by the user’s accessibility settings
densityDpi- The screen density expressed as dots-per-inch
Usage
Common Patterns
Responsive Layout
Dynamic Grid Columns
Orientation-Aware Layout
Accessibility-Aware Typography
Responsive Image Sizing
Modal Positioning
Dimensions vs useWindowDimensions
useWindowDimensions is preferred over the Dimensions API for components because:
- Automatic updates: The hook automatically re-renders your component when dimensions change
- React lifecycle: Properly integrates with React’s component lifecycle
- No manual cleanup: Subscriptions are managed automatically
Implementation Details
This hook:- Uses
useStateto store current dimensions - Subscribes to dimension changes in a
useEffect - Handles missed updates between initial render and subscription
- Filters out no-op updates when dimensions haven’t actually changed
- Automatically cleans up subscriptions on unmount
Platform Differences
iOS
Returnswidth, height, scale, and fontScale.
Android
Returnswidth, height, scale, fontScale, and densityDpi.
Window vs Screen
This hook returns window dimensions (the visible app area), not screen dimensions. On Android with split-screen, this reflects only your app’s window.Performance Considerations
- The hook efficiently prevents unnecessary re-renders by comparing dimension values
- Only updates state when dimensions actually change
- Subscription cleanup is automatic and efficient
See Also
- Dimensions API - Lower-level API for getting dimensions
- Responsive Design Guide - Best practices for responsive layouts