antd-mobile ListView長(zhǎng)列表的數(shù)據(jù)更新遇到的坑
遇到的問(wèn)題
listView這個(gè)組件我真的是看文檔看得腦殼疼。好不容易看文檔寫完長(zhǎng)列表數(shù)據(jù)展示了。然后遇到一個(gè)需求,即用戶有一個(gè)點(diǎn)贊操作,問(wèn)題出現(xiàn)了,點(diǎn)贊完數(shù)據(jù)更新之后listView不刷新列表。
解決列表不刷新問(wèn)題
官方的demo里有這么一個(gè)函數(shù) rowHasChanged ,這個(gè)函數(shù)返回true或者false,如果是true,則認(rèn)為這行數(shù)據(jù)改變了,然后刷新這行數(shù)據(jù),也就更新了列表。
// 官方 constructor(props) { super(props); ... const dataSource = new ListView.DataSource({ ... rowHasChanged: (row1, row2) => row1 !== row2 // 這個(gè)方法 }); }
然后就各種百度,最后在github上看到這個(gè) issue。最后大家得出的結(jié)論就是如果要繼續(xù)用這個(gè)組件,又想刷新列表的話就只能寫成下面這樣。
but,這樣寫會(huì)讓所有的數(shù)據(jù)都更新,對(duì)性能的消耗挺大的。
// !!!這樣寫 rowHasChanged: ( row1, row2) => true
emmm,但是我不想去看其他的插件了,所以就采用了上面的寫法。
下面就講一下我怎么配置這個(gè)listView的,因?yàn)槲矣X得這個(gè)組件官方demo還真的寫得蠻看不懂的。
ListView在實(shí)際項(xiàng)目中使用
下面的代碼主要展示怎么配置listview,不要扣小地方,因?yàn)槲野押芏鄻I(yè)務(wù)代碼去掉了。
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() { // 請(qǐng)求初始化數(shù)據(jù) } // 在這里維護(hù)長(zhǎng)列表數(shù)據(jù),把從接口獲取來(lái)的數(shù)據(jù)賦值給state里的dataSource。 componentWillReceiveProps(nextProps) { if(nextProps.message.commentList !== this.props.message.commentList){ this.setState({ // 注意!這里的cloneWithRows(),antd里規(guī)定用它來(lái)更新dataSource,這個(gè)不是拼接數(shù)據(jù),用這個(gè)函數(shù),dataSource會(huì)更新成nextProps.message.commentList。 //所以在接受后端分頁(yè)數(shù)據(jù)時(shí),就把拼接好的數(shù)據(jù)賦值給nextProps.message.commentList(這個(gè)在model.js里寫了) dataSource: this.state.dataSource.cloneWithRows(nextProps.message.commentList), }); } } // onEndReached,列表被滾動(dòng)到距離最底部不足`onEndReachedThreshold`個(gè)像素的距離時(shí)調(diào)用 // 在這里寫分頁(yè)請(qǐng)求 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}>點(diǎn)贊</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>列表頭,什么寫點(diǎn)什么</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) { // 長(zhǎng)列表,上一次頁(yè)的數(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長(zhǎng)列表的數(shù)據(jù)更新遇到的坑的詳細(xì)內(nèi)容,更多關(guān)于antd-mobile ListView長(zhǎng)列表的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
js利用Array.splice實(shí)現(xiàn)Array的insert/remove
從一個(gè)數(shù)組中移除一個(gè)或多個(gè)元素,如果必要,在所移除元素的位置上插入新元素,返回所移除的元素。2009-01-01uni-app動(dòng)態(tài)修改導(dǎo)航欄標(biāo)題簡(jiǎn)單步驟
uniapp作為一款開源軟件,可以做到一端多用,不過(guò)也有局限,在開發(fā)中有時(shí)候需要?jiǎng)討B(tài)的去修改標(biāo)題,下面這篇文章主要給大家介紹了關(guān)于uni-app動(dòng)態(tài)修改導(dǎo)航欄標(biāo)題的相關(guān)資料,需要的朋友可以參考下2023-06-06JavaScript html5利用FileReader實(shí)現(xiàn)上傳功能
這篇文章主要為大家詳細(xì)介紹了JavaScript html5利用FileReader實(shí)現(xiàn)上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03JavaScript網(wǎng)絡(luò)請(qǐng)求工具庫(kù)axios使用實(shí)例探索
這篇文章主要為大家介紹了JavaScript網(wǎng)絡(luò)請(qǐng)求工具庫(kù)axios使用實(shí)例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01深入探討JavaScript異步編程中Promise的關(guān)鍵要點(diǎn)
這篇文章將全面深入地探討Promise,包括其前身、歷史、能力、優(yōu)點(diǎn)、缺點(diǎn)以及提供每個(gè)方法的案例,感興趣的小伙伴可以跟隨小編一學(xué)習(xí)一下2023-06-06如何在JavaScript中使用localStorage詳情
這篇文章主要介紹了如何在JavaScript中使用localStorage,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02