React-Native scroll to top with Flatlist

FOR REACT HOOKS

  1. import React, {useRef} from 'react'
  2. declare it -> const flatListRef = useRef()
  3. set it like ref={flatListRef}
  4. 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