【发布时间】:2017-12-20 16:06:05
【问题描述】:
这是我的简单category list page 的 render() 函数。
最近我为我的FlatList 视图添加了分页,所以当用户滚动到底部时,onEndReached 在某个点被调用(onEndReachedThreshold 从底部开始的值长度),它将获取下一个@987654326 @ 并连接类别道具。
但我的问题是onEndReached 是在调用render() 时调用 换句话说,FlatList 的onEndReached 在到达底部之前就被触发了。
我为onEndReachedThreshold? 设置了错误的值吗?您发现有什么问题吗?
return (
<View style={{ flex:1 }}>
<FlatList
data={this.props.categories}
renderItem={this._renderItem}
keyExtractor={this._keyExtractor}
numColumns={2}
style={{flex: 1, flexDirection: 'row'}}
contentContainerStyle={{justifyContent: 'center'}}
refreshControl={
<RefreshControl
refreshing = {this.state.refreshing}
onRefresh = {()=>this._onRefresh()}
/>
}
// curent value for debug is 0.5
onEndReachedThreshold={0.5} // Tried 0, 0.01, 0.1, 0.7, 50, 100, 700
onEndReached = {({distanceFromEnd})=>{ // problem
console.log(distanceFromEnd) // 607, 878
console.log('reached'); // once, and if I scroll about 14% of the screen,
//it prints reached AGAIN.
this._onEndReachedThreshold()
}}
/>
</View>
)
更新我在这里获取this.props.categories 数据
componentWillMount() {
if(this.props.token) {
this.props.loadCategoryAll(this.props.token);
}
}
【问题讨论】:
-
onEndReachedThreshold取值范围是 0 和 1,其中 0 是滚动的顶部,1 是滚动的末尾。首先,我会尝试添加 0.7 并检查它是否按预期工作。让我知道它是否有帮助 -
谢谢!但是 0.7 并没有解决问题。还是在第一时间触发onEndReached
-
拥有这样的类变量怎么样。_mounted?
-
如果是这种情况,您可以尝试添加一些条件渲染以仅在您的
categories填充了值之后渲染您的FlatList。像这样的东西:categories.length > 0 ? <FlatList... /> : < ActivityIndicator /> -
您可以尝试在您的
<View>中添加此条件。像这样的东西:<View> {categories ? <FlatList ... > : <Image ... /> } </View>。您可以在此处找到更多示例:reactjs.org/docs/conditional-rendering.html 让我知道它是否有效
标签: react-native