FlatList - 如何自动到顶部

FlatList - How to go automatically to the top

我正在使用 React Native (Expo)。

我能做什么:

现在,例如,如果用户在更改屏幕并返回到该列表时转到该列表的底部,他仍然在底部。

有什么建议吗?

您可以在屏幕上添加一个 event listener 并像这样调用 scrollToTop

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

  return unsubscribe;
}, [navigation]);