我試圖訪問this.refs,所以我可以滾動到平面列表的頂部,但它是未定義的。React Native - 如何訪問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>
)
}
}
正如你所看到的,我有裁判設置爲「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函數內部不能是未定義?
https://facebook.github.io/react/docs/refs-and -the-dom.html – euvl
@euvl是的,這不會幫助我。我已經讀過 – Tenatious
,如果你嘗試使用'ref = {flatList => this.flatList = flatList}'並且在你的函數中使用'this.flatList',它是否會起作用? –