useHooks.iov4.1.2
DocsBlogGitHub
Hooks
No hooks found in any category.

useWindowSize

Browser

Installation

npx usehooks-cli@latest add use-window-size

Description

A React hook for tracking the browser window dimensions with automatic updates on resize events and SSR compatibility.

Return Type

WindowSize
PropertyTypeDescription
widthnumberCurrent window width in pixels
heightnumberCurrent window height in pixels

Examples

Responsive Component Behavior

Adapting component layout based on window size

1const { width, height } = useWindowSize(); 2const isMobile = width < 768; 3const isTablet = width >= 768 && width < 1024; 4const isDesktop = width >= 1024; 5 6return ( 7 <div className={`container ${isMobile ? 'mobile' : isTablet ? 'tablet' : 'desktop'}`}> 8 <h1>Current Window Size</h1> 9 <p>Width: {width}px</p> 10 <p>Height: {height}px</p> 11 <p>Device: {isMobile ? 'Mobile' : isTablet ? 'Tablet' : 'Desktop'}</p> 12 13 {isMobile && ( 14 <div>Mobile-specific content</div> 15 )} 16 17 {isDesktop && ( 18 <div className="desktop-sidebar"> 19 Desktop sidebar content 20 </div> 21 )} 22 </div> 23);

Dynamic Grid Layout

Adjusting grid columns based on available width

1const { width } = useWindowSize(); 2 3const getGridColumns = (windowWidth) => { 4 if (windowWidth < 640) return 1; 5 if (windowWidth < 1024) return 2; 6 if (windowWidth < 1280) return 3; 7 return 4; 8}; 9 10const columns = getGridColumns(width); 11 12return ( 13 <div 14 className="grid" 15 style={{ 16 gridTemplateColumns: `repeat(${columns}, 1fr)`, 17 gap: '1rem' 18 }} 19 > 20 {items.map((item, index) => ( 21 <div key={index} className="grid-item"> 22 {item.content} 23 </div> 24 ))} 25 </div> 26);

Conditional Rendering with Breakpoints

Showing different components based on screen size

1const { width } = useWindowSize(); 2 3const breakpoints = { 4 sm: 640, 5 md: 768, 6 lg: 1024, 7 xl: 1280 8}; 9 10const Navigation = () => { 11 if (width < breakpoints.md) { 12 return <MobileNavigation />; 13 } 14 15 return <DesktopNavigation />; 16}; 17 18const Sidebar = () => { 19 // Only show sidebar on larger screens 20 if (width < breakpoints.lg) { 21 return null; 22 } 23 24 return ( 25 <aside className="sidebar"> 26 <SidebarContent /> 27 </aside> 28 ); 29}; 30 31return ( 32 <div className="app-layout"> 33 <Navigation /> 34 <main className="main-content"> 35 <Content /> 36 </main> 37 <Sidebar /> 38 </div> 39);

Dependencies

react

Notes

  • Returns { width: 0, height: 0 } during SSR to prevent hydration mismatches
  • Automatically updates when the window is resized
  • Uses window.innerWidth and window.innerHeight for accurate viewport dimensions
  • Properly cleans up event listeners on component unmount
  • Calls resize handler immediately to get initial window size
  • Safe to use in SSR environments like Next.js

Implementation

1'use client'; 2 3import { useState, useEffect } from "react"; 4 5interface WindowSize { 6 width: number; 7 height: number; 8} 9 10function useWindowSize(): WindowSize { 11 const [windowSize, setWindowSize] = useState<WindowSize>(() => { 12 // Check if we're in a browser environment 13 if (typeof window !== "undefined") { 14 return { 15 width: window.innerWidth, 16 height: window.innerHeight, 17 }; 18 } 19 // Default values for SSR 20 return { 21 width: 0, 22 height: 0, 23 }; 24 }); 25 26 useEffect(() => { 27 // Only run on client side 28 if (typeof window === "undefined") { 29 return; 30 } 31 32 function handleResize() { 33 setWindowSize({ 34 width: window.innerWidth, 35 height: window.innerHeight, 36 }); 37 } 38 39 // Add event listener 40 window.addEventListener("resize", handleResize); 41 42 // Call handler right away so state gets updated with initial window size 43 handleResize(); 44 45 // Remove event listener on cleanup 46 return () => window.removeEventListener("resize", handleResize); 47 }, []); 48 49 return windowSize; 50} 51 52export default useWindowSize; 53