Support
Usage
Track scroll position
Programmatic scroll
Notes
- Children must have
.id()set for scroll position tracking to work. - Use with
.scrollTargetLayout()and.scrollTargetBehavior()for snapping scroll views.
Tracks and controls scroll position by child view ID.
.scrollPosition(props: {
id: string | null;
setId: (value: string | null) => void;
}): Component
const body = () => {
const [scrollId, setScrollId] = useState(null)
return ScrollView([
ForEach(items, (item) =>
Text(item.name)
.padding(16)
.id(item.id)
)
]).scrollPosition({ id: scrollId, setId: setScrollId })
}
const body = () => {
const [scrollId, setScrollId] = useState(null)
return VStack([
Button("Scroll to top", () => setScrollId("item-0")),
ScrollView([
ForEach(items, (item) =>
Text(item.name)
.padding(16)
.id(item.id)
)
]).scrollPosition({ id: scrollId, setId: setScrollId })
])
}
.id() set for scroll position tracking to work..scrollTargetLayout() and .scrollTargetBehavior() for snapping scroll views.