Javascript 反应本机scrollview onScroll调用函数以呈现更多元素

Javascript 反应本机scrollview onScroll调用函数以呈现更多元素,javascript,react-native,infinite-scroll,Javascript,React Native,Infinite Scroll,因此,我用实现了自己的无限滚动类型thingo,在确定用户是否在屏幕上滚动了一半时遇到了麻烦。如果滚动到一半,则handlescroll函数中的if语句有时会同时触发(取决于滚动到一半的次数) 有没有人做过类似的事情,如果有,你能告诉我你是如何做到的 这是我的scrollview,它只包含许多post组件 return ( <ScrollView key={"scrollview_1"} onScroll={this.handleScroll.bind(this)}>

因此,我用
实现了自己的
无限滚动
类型thingo,在确定用户是否在屏幕上滚动了一半时遇到了麻烦。如果滚动到一半,则
handlescroll
函数中的if语句有时会同时触发(取决于滚动到一半的次数)

有没有人做过类似的事情,如果有,你能告诉我你是如何做到的

这是我的scrollview,它只包含许多post组件

return (  
      <ScrollView key={"scrollview_1"} onScroll={this.handleScroll.bind(this)}>
        { postComponents }
      </ScrollView>
)


handleScroll (event: Object) {
    console.log(event.nativeEvent.contentOffset.y);
    const offset = event.nativeEvent.contentOffset.y;
    const screenHeight = Dimensions.get('window').height;

    const {FeedActions,} = this.props;
    const {nextUrl,} = feed;

    // Somehow figure out a way to check if past halfway only once 
    var x = (offset % screenHeight/2);
    if( x > 0 && x < 40){
      // Run API call get more posts, then render the new components
      FeedActions.getMorePosts(nextUrl, true);
    }

  }
返回(
{后组件}
)
handleScroll(事件:对象){
log(event.nativeEvent.contentOffset.y);
const offset=event.nativeEvent.contentOffset.y;
const screenHeight=维度.get('window')。高度;
const{FeedActions,}=this.props;
const{nextUrl,}=feed;
//不知何故,想出一个办法来检查是否只有一次通过了一半
变量x=(偏移量%屏幕高度/2);
如果(x>0&&x<40){
//运行API调用获取更多帖子,然后呈现新组件
FeedActions.getMorePosts(nextUrl,true);
}
}

我还发现,如果它们滚动过一半&只滚动过
0(偏移量%screenheight/2)
,我的
FeedActions.getMorePosts
功能将再次运行。为了解决这个问题,我正在考虑实现一个
状态
,它将保存一个过去屏幕高度的数组。在if语句中,我将检查我的状态数组中是否已经过了任何屏幕高度。

使用ListView而不是ScrollView是否更适合您的用例?你可以使用像
onEndReached
@Bhullnatik这样的方法-我以前从未使用过它。不过我会调查的