A small, type-safe utility for **scrolling to items by key** in React Native.
npm install react-native-scroll-registryA small, type-safe utility for scrolling to items by key in React Native.
It provides a single mental model that works for both:
- ScrollView (measure-based scrolling)
- FlatList (index-based, virtualization-safe scrolling)
> Register items with a key → call scrollTo(key).
---
- Unified API for ScrollView and FlatList
- Works with vertical and horizontal lists
- Safe for large, virtualized lists (FlatList)
- Zero dependencies
- TypeScript-first
- No native code
---
``sh`
yarn add react-native-scroll-registry
or
`sh`
npm install react-native-scroll-registry
There are only two steps:
1. Register items with a unique key
2. Scroll to that key
Internally:
- ScrollView → uses layout measurementFlatList
- → uses scrollToIndex
You don’t need to care which one is used.
---
Creates a shared registry for scroll targets.
`ts`
const { registry, registerView, registerIndex } = useScrollRegistry();
| Function | Used for | Description |
|------|------|------|
| registerView(key) | ScrollView | Registers a view ref |registerIndex(key, index)
| | FlatList | Registers an item index |
---
Returns a scrollTo function.
`ts`
const scrollTo = useScrollTo({
scrollViewRef,
containerRef,
registry,
axis: "vertical"
});
#### Options
| Option | Description |
|------|------|
| scrollViewRef | Ref to ScrollView |flatListRef
| | Ref to FlatList |containerRef
| | Required for ScrollView |registry
| | Registry from useScrollRegistry |axis
| | "vertical" (default) or "horizontal" |
---
`tsx
const scrollRef = useRef
const containerRef = useRef
const { registry, registerView } = useScrollRegistry();
const scrollTo = useScrollTo({
scrollViewRef: scrollRef,
containerRef,
registry,
});
{items.map(item => (
))}
---
`tsx
const listRef = useRef
const { registry, registerIndex } = useScrollRegistry();
const scrollTo = useScrollTo({
flatListRef: listRef,
registry,
});
data={data}
renderItem={({ item, index }) => {
registerIndex(item.id, index);
return
}}
/>
---
Just set the axis:
`ts`
useScrollTo({ axis: "horizontal", ... })
Works for both ScrollView and FlatList.
---
- Use keys that are stable (IDs, not array indices)
- ScrollView requires all items to be mountedFlatList
- is recommended for large listsregisterView
- Do not mix and registerIndex` for the same list