React useScreen Hook
React useScreen hook for screen information tracking. Monitor display dimensions, orientation, and capabilities with TypeScript support for Next.js apps.
Need help with screen detection?
Join our Discord community for help from other developers.
Ever tried to detect screen properties in React and ended up with window.screen access confusion, missing orientation change events, or broken multi-monitor support? You know the drill—manually checking window.screen properties, forgetting to listen for orientation changes, dealing with SSR hydration mismatches, missing debouncing for performance. This free open source React useScreen custom hook handles all that screen detection complexity so you can focus on building responsive features instead of debugging display property edge cases in your React applications.
useScreen showcase
Real-time screen property tracking with orientation and dimension monitoring:
This free open source React hook simplifies screen information access with TypeScript support for modern JavaScript applications. Whether you're building device-adaptive layouts, orientation-aware interfaces, or display-specific features in your Next.js projects, this React hook keeps your screen data accurate and reactive.
Installation
npx shadcn@latest add https://www.shadcn.io/registry/use-screen.json
npx shadcn@latest add https://www.shadcn.io/registry/use-screen.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/use-screen.json
bunx shadcn@latest add https://www.shadcn.io/registry/use-screen.json
Why most screen detection implementations suck
Look, you could keep accessing window.screen directly and manually listening for orientation changes. But then you hit the screen detection complexity—missed orientation events, performance issues from unthrottled updates, SSR incompatibility, inconsistent multi-monitor behavior in React applications.
Most developers access window.screen properties directly without proper event handling, missing crucial orientation changes when users rotate devices in TypeScript components. Or they poll screen properties without debouncing, causing performance issues from constant re-renders. Some forget about SSR considerations entirely and get hydration mismatches when screen properties differ between server and client in Next.js projects.
This React hook uses optimized event listening under the hood, with automatic orientation change detection and proper SSR handling in JavaScript applications. The browser handles all the screen events, plus you get debounced updates and comprehensive property tracking in one call.
Plus it handles all the edge cases—SSR compatibility with controlled hydration, orientation API fallbacks, automatic cleanup and memory management in React development. No more scattered screen property checks or broken device rotation handling.
This free open source React hook manages screen state while you focus on building features. Whether you're creating React applications, Next.js interfaces, or TypeScript components, reliable screen detection keeps your JavaScript development adaptive.
Features
- Screen properties tracking providing comprehensive display information including dimensions and color depth in React applications
- Orientation support with automatic updates for device rotation and screen changes in TypeScript components
- Debounced updates allowing performance optimization for high-frequency resize events in Next.js projects
- SSR compatibility with proper server-side rendering support and initialization options in JavaScript development
- TypeScript overloads providing type-safe usage patterns for different initialization modes for React frameworks
- Memory efficient using proper cleanup and event listener management in modern applications
- Free open source designed for modern React development workflows
When you'll actually use this
Real talk—this isn't for basic responsive design in React applications. CSS media queries and viewport units handle most screen-based styling perfectly. But when you need actual screen hardware information for device detection or multi-monitor support, this React hook delivers in Next.js projects.
Perfect for:
- Device capability detection - Check screen resolution and color depth for optimal graphics built with TypeScript
- Multi-monitor applications - Detect multiple displays and available screen real estate using React patterns
- Orientation-aware interfaces - Respond to device rotation with layout changes in JavaScript applications
- High-DPI optimization - Adapt content quality based on pixel density in React components
- Analytics and monitoring - Track user display capabilities for optimization insights in Next.js applications
- Kiosk or fullscreen apps - Utilize full screen dimensions for immersive experiences using TypeScript safety
API Reference
useScreen
useScreen<T extends boolean = true>(
options?: UseScreenOptions<T>
): T extends false ? Screen | undefined : Screen
Parameter | Type | Default | Description |
---|---|---|---|
options | UseScreenOptions<T> | {} | Configuration options for screen monitoring |
UseScreenOptions
Property | Type | Default | Description |
---|---|---|---|
initializeWithValue | boolean | true | Whether to initialize with screen values immediately (set to false for SSR) |
debounceDelay | number | undefined | Delay in milliseconds for debounced updates to improve performance |
Screen Properties
Property | Type | Description |
---|---|---|
width | number | Total screen width in pixels (physical display size) |
height | number | Total screen height in pixels (physical display size) |
availWidth | number | Available screen width excluding taskbars and system UI |
availHeight | number | Available screen height excluding taskbars and system UI |
colorDepth | number | Color depth in bits per pixel (usually 24 or 32) |
pixelDepth | number | Pixel depth in bits per pixel (legacy, usually same as colorDepth) |
orientation | ScreenOrientation | null | Screen orientation object (if supported by browser) |
ScreenOrientation Properties
Property | Type | Description |
---|---|---|
type | string | Orientation type: 'portrait-primary', 'portrait-secondary', 'landscape-primary', 'landscape-secondary' |
angle | number | Orientation angle in degrees: 0, 90, 180, or 270 |
Common gotchas
Screen vs viewport are different concepts: This React hook tracks physical screen hardware properties, not browser viewport in React applications. For responsive web design, use viewport-based approaches instead of screen dimensions.
Orientation API has limited support: The screen.orientation object might be null on some browsers in TypeScript components. Always check for existence before accessing orientation properties.
Multi-monitor support is basic: The React hook reports the primary display's properties in Next.js projects. True multi-monitor detection requires additional browser APIs beyond the standard screen object.
SSR requires special handling: Use initializeWithValue: false
to prevent hydration mismatches in JavaScript applications. The hook will return undefined
initially and update with real data after client hydration.
Debouncing affects responsiveness: While debouncing improves performance, it can delay orientation change updates in React frameworks. Balance performance needs with responsiveness requirements.
Browser compatibility varies: Not all screen properties are available in every browser in TypeScript projects. Always provide fallbacks for missing properties or APIs.
Related hooks you will also like
useWindowSize
Window size tracking for viewport-based responsive behavior
useMediaQuery
Media query detection for responsive breakpoint logic
useResizeObserver
Element size detection for device-adaptive interfaces
useIsClient
Client detection needed for safe screen property access
useLocalStorage
Persistent storage for device preferences and capabilities
useDebounceValue
Debounced updates for screen orientation change handling
Questions you might have
React useResizeObserver Hook
React useResizeObserver hook for element resize detection. Monitor DOM element size changes with ResizeObserver API and TypeScript support for Next.js apps.
React useScript Hook
React useScript hook for dynamic script loading. Load third-party libraries with status tracking, caching, and error handling using TypeScript for Next.js.