0

I'm using React Native (Expo).

What can I do to:

  • Every time that one user enters in the screen with the respective flatlist automatically goes to the top of that list.

Now for example if a user goes to the bottom of that list when he changes the screen and go back to that one, he stills on the bottom.

Any suggestion?

Dário
  • 125
  • 1
  • 8
  • Can you detect when they come back, and run something like `this.flatListRef.scrollToOffset({ animated: true, offset: 0 });` ? – TKoL Jun 29 '21 at 16:36
  • https://stackoverflow.com/questions/50436635/react-native-scroll-to-top-with-flatlist – TKoL Jun 29 '21 at 16:36

1 Answers1

1

You can add an event listener to the screen and call scrollToTop like this

useEffect(() => {
  const unsubscribe = navigation.addListener('focus', () => {
    flatListRef.current.scrollToOffset({ animated: true, offset: 0 });
  });

  return unsubscribe;
}, [navigation]);
Kartikey
  • 4,516
  • 4
  • 15
  • 40