Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FlatList] Some questions about onEndReached #20284

Closed
sunhailongisme opened this issue Jul 19, 2018 · 1 comment
Closed

[FlatList] Some questions about onEndReached #20284

sunhailongisme opened this issue Jul 19, 2018 · 1 comment
Labels
Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.

Comments

@sunhailongisme
Copy link

         <FlatList
                        keyboardShouldPersistTaps={'handled'}
                        data={this.state.listData}
                        initialNumToRender={this.initialNumToRender}
                        keyExtractor={this._keyExtractor}
                        renderItem={this.getView}//渲染每一条记录
                         ListEmptyComponent={this.emptyComponent}
                        ListFooterComponent={this.footer}//尾部
                        //下拉刷新,必须设置refreshing状态
                        onRefresh={this.onRefresh}
                        refreshing={this.state.refreshing}
                        //上拉加载
                        onEndReachedThreshold={0.1}
                        onEndReached={this.onEndReached}
                    >
         </FlatList>


onEndReached  =() => {
    currentPage++;
    if (this.state.showFoot === 0){
      this.setState({refreshing:false});
    }
    params.reqParams.page = currentPage;
    if (this.state.data.totalpage&&this.state.data.totalpage !== undefined){
      if(currentPage == this.state.data.totalpage){
        this.setState({showFoot:1});//底部显示没有更多数据
      }else if(currentPage>this.state.data.totalpage){
        return
      }
    }
    Fetch.fetchData('jsonpost',{},(res) => {
      if (res.success!==undefined&&res.success === false){
        this.setState({msg:'连接超时,请重试!'});
        this.alertSearch._show();
      }
      if (res.respCode === 'success'){
        this.setState({
          listData:this.state.listData.concat(res.data.ENTERPRISES),
          showFoot:0,
          refreshing:false,
          totalnum:res.totalnum,
        });
      }else if(res.respCode === 'failed'){
        this.setState({msg:res.Errmsg,refreshing:false});
        this.alertSearch._show();
      }
    })
};

onEndReached triggered 2 times on ios ,but,it is normal on android .If I change a way of writing:

onEndReached  =() => {
    currentPage++;
    if (this.state.showFoot === 0){
      this.setState({refreshing:false});
    }
    params.reqParams.page = currentPage;
    if (this.state.data.totalpage&&this.state.data.totalpage !== undefined){
      if(currentPage == this.state.data.totalpage){
        this.setState({showFoot:1});//底部显示没有更多数据
      }else if(currentPage>this.state.data.totalpage){
        return
      }
    }
    Fetch.fetchData('jsonpost',{},(res) => {
      if (res.success!==undefined&&res.success === false){
        this.setState({msg:'连接超时,请重试!'});
        this.alertSearch._show();
      }
      if (res.respCode === 'success'){
       const getNewData = (res) =>{
         let oldData = this.state.listData;
         for (let i = 0;i<res.data.ENTERPRISES.length;i++){
          oldData.push(res.data.ENTERPRISES[i]);
         }
          return oldData 
        }
        const newData = getNewData(res);
        this.setState({
          listData:newData,
          showFoot:0,
          refreshing:false,
          totalnum:res.totalnum,
        });
      }else if(res.respCode === 'failed'){
        this.setState({msg:res.Errmsg,refreshing:false});
        this.alertSearch._show();
      }
    })
};

The method of "onEndReached" on IOS will not be executed two times.But there will be another problem,
When I first scrolled to the bottom of the list to trigger the "onEndReached" method, if the network request failed and no new list data was rendered, I scrolled again to the bottom of the list and the "onEndReached" method could not be triggered again

The problems of the above two ways of writing:

1、When I am not on the first page list, if I afresh render the list, It will still automatically execute a "onEndReached" method, but I do not scroll to trigger the "onEndReached" method 。
2、When the first render list is less than one screen, the "onEndReached" method will still execute automatically.

I really don't know why these problems will arise. If anyone knows the reasons, please explain them.

@react-native-bot
Copy link
Collaborator

We are automatically closing this issue because it does not appear to follow any of the provided issue templates.

Please make use of the bug report template to let us know about a reproducible bug or regression in the core React Native library.

If you'd like to start a discussion, check out https://discuss.reactjs.org or follow the discussion template.

@react-native-bot react-native-bot added Ran Commands One of our bots successfully processed a command. 📋No Template labels Jul 19, 2018
@facebook facebook locked as resolved and limited conversation to collaborators Jul 19, 2019
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

2 participants