React-Native scroll to top with Flatlist
FOR REACT HOOKS
import React, {useRef} from 'react'
- declare it ->
const flatListRef = useRef()
- set it like
ref={flatListRef}
- call it like
flatListRef.current.scrollToOffset({animated: false, offset: 0})
The correct syntax is
this.flatListRef.scrollToOffset({ animated: true, offset: 0 });
and you can also use
scrollToIndex
Just in case someone is lost on how to do this with hooks, here is an example
function MyComponent() {
const flatListRef = React.useRef()
const toTop = () => {
// use current
flatListRef.current.scrollToOffset({ animated: true, offset: 0 })
}
return (
<FlatList
ref={flatListRef}
data={...}
...
/>
)
}
The main difference is that you access it by .current