user15322469
user15322469

Reputation: 909

how can i use scrollToIndex in FlatList React native?

what i'm trying to do is that when i run onPress, i want to scroll down as much as value of y (which is 499)

but if i use my code it doesn't work anything

this is my code

    const TodoList = ({}) => {

      const height = Dimensions.get('window').height;
      const flatListRef = useRef()

    
      return (
        <FlatList
        ref={flatListRef}
          style={{height}}
          keyExtractor={(item) => String(item.id)}

          renderItem={({item}) => (
            <TodoItem
            onPress={() => {
              const node = flatListRef.current;
              if (node) {
                node.scrollToIndex({x: 0, y: 499, animated: true, index:0});
              }
            }}
            />
          )}
        />

what code should i fix?...

Upvotes: 2

Views: 414

Answers (1)

Carlos J
Carlos J

Reputation: 3045

I think you are using the wrong method. You should be using scrollToOffset

if (node) {
    node.scrollToOffset({ offset: 499, animated: true });
}

Upvotes: 1

Related Questions