useScrollRestoration
When rendered inside a RouterProvider, will restore scroll positions on navigations.
Parameters
A function that returns a key to use for scroll restoration. This is useful for custom scroll restoration logic, such as using only the pathname so that subsequent navigations to prior paths will restore the scroll.Defaults to
location.key.The key to use for storing scroll positions in
sessionStorage.Type Declaration
Deprecation Notice
This hook is deprecated in favor of the<ScrollRestoration> component. The component approach is preferred because:
- Better SSR support: The component can inject the necessary script for SSR scroll restoration
- Simpler API: No need to manually call the hook in your components
- Automatic setup: Just render the component and it works
Migration Guide
Before (using useScrollRestoration)
After (using ScrollRestoration component)
Usage Examples (Legacy)
Basic Usage
Custom Storage Key
Pathname-Based Restoration
Route-Based Restoration
Conditional Restoration
How It Works
The hook:- Saves scroll positions to
sessionStoragewhen navigating away from a page - Restores scroll positions when navigating back to a previously visited page
- Uses
location.keyby default to track scroll positions for each history entry - Allows customization via the
getKeyfunction for different restoration strategies
Alternatives
Use ScrollRestoration Component (Recommended)
Manual Scroll Management
Prevent Scroll Reset on Specific Links
Notes
- Deprecated: Use the
<ScrollRestoration>component instead - Available in Framework and Data modes only
- Must be used within a
RouterProvider - Stores scroll positions in
sessionStorage - The
getKeyfunction allows customizing which navigations restore scroll - Returning
nullfromgetKeyprevents scroll restoration for that navigation
Related
<ScrollRestoration>- Recommended replacement componentpreventScrollReset- Prevent scroll reset on specific navigationsuseLocation- Get current location- Scroll Restoration Guide - Learn about scroll restoration patterns