我正在尝试访问 this.refs 以便可以滚动到平面列表的顶部,但它是未定义的。
我的渲染方法是这样的:
render() {
return (
<View style={styles.container}>
{ this.state.loading &&
<ActivityIndicator />
}
{ !this.state.loading &&
<FlatList
ref='flatList'
data={this.state.facebookPosts}
onEndReachedThreshold={0}
onEndReached={({ distanceFromEnd }) => {
this._getFacebookPosts(this.state.nextPost);
}}
renderItem={({item}) => <FacebookPost
date={item.created_time}
message={item.message}
image={item.attachments.data[0].media.image.src}
url={item.link}
/>}
/>
}
</View>
)
}
}
如您所见,我将 ref 设置为“flatList”。
然后我有一个选项卡栏,当您单击其中一个选项卡时,它会滚动到列表顶部。(我只是为了不控制台注销 this.refs 但未定义)
static navigationOptions = {
tabBarLabel: 'News',
showIcon: true,
tabBarIcon: ({ tintColor }) => (
<Image
source={require('../assets/images/newspaper-icon.png')}
style={[styles.icon, {tintColor: tintColor}]}
/>
),
tabBarOnPress: (scene, jumpToIndex) => {
// this.refs.listRef.scrollToOffset({x: 0, y: 0, animated: true})
console.log(this.refs);
jumpToIndex(scene.index);
}
};
我需要做些什么不同的事情才能使 this.refs 在 onTabBarPress 函数中不被未定义?