我在 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/