Register the current route as a scroll group. Child routes will preserve scroll position when navigating between them.
This is useful for layouts where you want to maintain scroll position while users navigate between child routes, such as:
Use this hook in a layout component to create a scroll group for all its children:
// app/dashboard/_layout.tsx
import { useScrollGroup, Slot } from 'one'
export default function DashboardLayout() {
// All routes under /dashboard will share scroll position
useScrollGroup()
return (
<div>
<nav>Dashboard Navigation</nav>
<Slot />
</div>
)
}
When users navigate between /dashboard/overview, /dashboard/settings, and /dashboard/reports, the scroll position of the dashboard layout will be preserved.
By default, useScrollGroup uses the current pathname. You can specify a custom group path:
// Create a scroll group for a specific path prefix
useScrollGroup('/products')
You can also specify a scroll group when navigating programmatically:
import { useRouter } from 'one'
function MyComponent() {
const router = useRouter()
const goToSettings = () => {
router.navigate('/dashboard/settings', {
scrollGroup: '/dashboard'
})
}
return <button onClick={goToSettings}>Go to Settings</button>
}
useScrollGroup(), it registers the current path as a scroll groupThis hook only affects web scroll behavior. On native platforms, it's a no-op since React Navigation handles scroll restoration differently.
function useScrollGroup(groupPath?: string): void
| Parameter | Type | Description |
|---|---|---|
groupPath | string (optional) | Custom path to use as the scroll group. Defaults to current pathname. |
Edit this page on GitHub.