溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

antd-mobile ListView長列表的數(shù)據(jù)更新時常見問題

發(fā)布時間:2021-03-10 15:41:20 來源:億速云 閱讀:423 作者:TREX 欄目:web開發(fā)

本篇內(nèi)容主要講解“antd-mobile ListView長列表的數(shù)據(jù)更新時常見問題”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“antd-mobile ListView長列表的數(shù)據(jù)更新時常見問題”吧!


遇到的問題

listView這個組件我真的是看文檔看得腦殼疼。好不容易看文檔寫完長列表數(shù)據(jù)展示了。然后遇到一個需求,即用戶有一個點贊操作,問題出現(xiàn)了,點贊完數(shù)據(jù)更新之后listView不刷新列表。

解決列表不刷新問題

官方的demo里有這么一個函數(shù) rowHasChanged ,這個函數(shù)返回true或者false,如果是true,則認為這行數(shù)據(jù)改變了,然后刷新這行數(shù)據(jù),也就更新了列表。

// 官方
 constructor(props) {
  super(props);
  ...
  const dataSource = new ListView.DataSource({
   ...
   rowHasChanged: (row1, row2) => row1 !== row2 // 這個方法
  });
 }

然后就各種百度,最后在github上看到這個 issue。最后大家得出的結論就是如果要繼續(xù)用這個組件,又想刷新列表的話就只能寫成下面這樣。

but,這樣寫會讓所有的數(shù)據(jù)都更新,對性能的消耗挺大的。

// !!!這樣寫
rowHasChanged: ( row1, row2) => true

emmm,但是我不想去看其他的插件了,所以就采用了上面的寫法。

下面就講一下我怎么配置這個listView的,因為我覺得這個組件官方demo還真的寫得蠻看不懂的。

ListView在實際項目中使用

下面的代碼主要展示怎么配置listview,不要扣小地方,因為我把很多業(yè)務代碼去掉了。

class Message extends React.Component {
 constructor(props) {
  super(props);
  const dataSource = new ListView.DataSource({
  // 這樣寫,每次都執(zhí)行rowHasChanged,每次都更新row
   rowHasChanged: ( row1, row2) => true
  });

  this.state = {
   dataSource,
  };
 }

 componentDidMount() {
  // 請求初始化數(shù)據(jù)
 }

 // 在這里維護長列表數(shù)據(jù),把從接口獲取來的數(shù)據(jù)賦值給state里的dataSource。
 componentWillReceiveProps(nextProps) {
  if(nextProps.message.commentList !== this.props.message.commentList){
   this.setState({
   // 注意!這里的cloneWithRows(),antd里規(guī)定用它來更新dataSource,這個不是拼接數(shù)據(jù),用這個函數(shù),dataSource會更新成nextProps.message.commentList。
   //所以在接受后端分頁數(shù)據(jù)時,就把拼接好的數(shù)據(jù)賦值給nextProps.message.commentList(這個在model.js里寫了)
    dataSource: this.state.dataSource.cloneWithRows(nextProps.message.commentList),
   });
  }
 }

// onEndReached,列表被滾動到距離最底部不足`onEndReachedThreshold`個像素的距離時調(diào)用
// 在這里寫分頁請求
 onEndReached = (event) => {
  const { dispatch } = this.props;
  const { email } = this.props.user;
  const { pageNum, pageSize, contentId, totalCount, commentList } = this.props.message;
  
  let hasMore = totalCount > commentList.length ? true : false;
  // load new data
  // hasMore: from backend data, indicates whether it is the last page, here is false
  if (!hasMore) {
   return;
  }
  dispatch({
   type: "message/updateStates",
   payload: {
    pageNum: pageNum+1,
    isLoading: true,
    isLongList: true
   }
  })
  setTimeout(() => {
   dispatch({
    type: "message/getCommentsByContentId",
    payload: {
     contentId,
     identity: email, 
     pageNum: pageNum+1,
     pageSize
    }
   })
  }, 1000);
 }

 render() {
 // 列表的item
  const row = (rowData, sectionID, rowID) => {
   const item = rowData;
   return (
    <div className={styles.item} key={rowID}>
      <div onClick={toggleLike}>點贊</div>
      <div className={styles.content}>{item.content}</div>
      </div>
    </div>
   );
  };

  return (
   <Fragment>
     <ListView
     ref={el => this.lv = el}
     dataSource={this.state.dataSource}
     renderHeader={() => (
      <div className={styles.sub}>
       <span>列表頭,什么寫點什么</span>
      </div>
     )}
     renderFooter={() => (<div style={{ padding: 10, textAlign: 'center' }}>
      { isLoading ? '加載中' : '加載完畢'}
     </div>)}
     renderRow={row}
     className="am-list"
     pageSize={pageSize}
     useBodyScroll
     scrollRenderAheadDistance={500}
     onEndReached={this.onEndReached}
     onEndReachedThreshold={10}
    />
   </Fragment>
  );
 }
}

model.js

*getCommentsByContentId({ payload }, { call, put, select }) {
   const { data } = yield call(getCommentsByContentId, payload);
   const { message } = yield select(state=>state);
   const { commentList } = message;
   if (data.code === 200) {
    // 長列表,上一次頁的數(shù)據(jù)+這次的數(shù)據(jù),賦值給新的commentList
    let list = [...commentList, ...data.data.list]
    yield put({
     type: 'updateStates',
     payload: {
      totalCount: data.data.totalCount,
      commentList: list
     }
    });
   } else {
    Toast.fail(data.msg, 1)
   }
  },

到此,相信大家對“antd-mobile ListView長列表的數(shù)據(jù)更新時常見問題”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI