上拉加載更多及下拉刷新(Flatlist、SectionList)

思路

  • sectionList及FlatList都有屬性refreshControl、onEndReachedThreshold(距離底部還有多遠(yuǎn)觸發(fā)加載更多)、onEndReached(加載更多加載事件)
  • 傳遞參數(shù)loadType區(qū)分加載事件,loadType為refresh下拉刷新事件,loadType為more加載更多事件
  • 傳遞參數(shù)page,page為1下拉刷新事件,this.state.page += 1
    為加載更多事件
  • react-redux框架:在reducer中發(fā)起網(wǎng)絡(luò)請(qǐng)求,添加狀態(tài)onRefresh,
    onRefresh根據(jù)傳過(guò)來(lái)的參數(shù)loadType判斷:onRefresh: loadType === refresh 下拉刷新?tīng)顟B(tài),為true,onMore: loadType === more 加載更多狀態(tài),為true,
  • 收到數(shù)據(jù): onRefresh 為false, onMore為false, 添加狀態(tài)hasMore, hasMore: 接收到的數(shù)據(jù)的長(zhǎng)度/pageSize === 傳過(guò)來(lái)的page,為true 還有更多數(shù)據(jù),為false沒(méi)有更多數(shù)據(jù)了

代碼

前端js界面

_onMore = () => {
        // 有更多數(shù)據(jù)且不是正在加載更多狀態(tài)下,請(qǐng)求更多數(shù)據(jù)
        if (this.props.message.hasMore && !this.props.message.onMore) {
            const {action, navigation, message} = this.props;

            this.state.page = this.state.page + 1;
            this.state.loadType = PageConstant.more;

            let params = {
                page: this.state.page,
                loadType: this.state.loadType,
            };
            action.requestMyMessages(navigation, message.messageArray, params, this._onLoad);
        }
    };

_onRefresh = () => {
        if (!this.props.message.onRefresh) {
            const {action, navigation} = this.props;
            this.state.page = 1;
            this.state.loadType = PageConstant.refresh;

            let params = {
                page: 1,
                loadType: this.state.loadType,
            };
            action.requestMyMessages(navigation, [], params, this._onLoad)
        }
    };

reducer

case types.my.requestMyMessages:
          return {
              ...state,
              navigation: action.navigation,
              params: action.params,
              array: action.array,
              onRefresh: action.params.loadType === PageConstant.refresh,
              onMore: action.params.loadType === PageConstant.more,
              callback: action.callback,
          };
      case types.my.receiveMyMessages:
          return {
              ...state,
              onRefresh: false,
              onMore: false,
              hasMore: action.messageArray.length / PageConstant.pageSize === state.params.page,
              messageArray: action.messageArray,
              onSubmit: false,
          };
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容