android - 在 React Native 中加载屏幕时如何滚动到顶部?

标签 android reactjs react-native react-native-flatlist

我在 HomeScreen 中使用 Flatlist,它显示了多个帖子。所以现在我想要的是每当我从应用程序注销或关闭应用程序然后打开应用程序时,我应该能够看到 Flatlist 中的第一个项目。

我尝试在我的渲染函数中使用 scrollToIndex 但它给了我一个错误 - undefined is not an object (evaluating '_this2.refs.flatListRef.scrollToIndex')

<FlatList 
    data={this.state.data}
    ref={(ref) => { this.flatListRef = ref; }}
    renderItem={ ({item,index}) =>  this._renderItem(item,index) }
    extraData={[ this.state.data, this.state.checked ]}
/>

这就是我在 componentDidMount 和渲染函数 this.refs.flatListRef.scrollToIndex({animated: true,index:0}); 中尝试使用的,但是没用。

最佳答案

ComponentDidMount 回调不会在用户离开应用程序并稍后恢复后运行。你必须使用 AppState相反:

AppState can tell you if the app is in the foreground or background, and notify you when the state changes. [Source]

根据您的需要调整给定示例并使用 this.flatListRef.scrollToIndex({animated: true,index:0})} 滚动

关于android - 在 React Native 中加载屏幕时如何滚动到顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54574942/

相关文章:

android - React Native 不考虑新的变化

android - 从加速度计数据检测设备运动方向?

android - 定期更新小部件 TextView 的简单方法

javascript - HTTP 无法识别正文

typescript - 平台特定的导入组件与 typescript native react

ios - 如何选择要通过NPM Start运行tvOS版本的项目

Android 将 findViewById 的上下文从内联类更改为 super

android - 更改填充所有编辑文本字段的按钮颜色?

javascript - React 测试库 waitForElementToBeRemoved 无法正常工作

javascript - 使用 Immutable 更新 React 状态