Overview
useWindowScroll tracks the current window scroll position and provides a helper to scroll the window with smooth behavior. Position is refreshed on scroll and resize, then initialized after mount.
Installation
Import
Usage
Basic Scroll Tracking
Scroll to Top Button
Horizontal Scrolling
Scroll Progress Indicator
API Reference
Function to scroll the window smoothlyAccepts partial position with
x and/or y propertiesType Definitions
Caveats
- Returns
{ x: 0, y: 0 }in SSR/SSG contexts wherewindowis undefined - Scroll function uses
behavior: 'smooth'by default - Position updates on both
scrollandresizeevents
SSR and RSC Notes
- Use this hook in Client Components only
- Do not call it from React Server Components
- Safe to use in SSR contexts (returns zero values until hydration)