ReactNative 吸頂 FlatList 白屏

項目中使用FlatList來實現(xiàn)“吸頂”的效果,默認(rèn)是第二行吸頂,故設(shè)置了stickyHeaderIndices屬性

現(xiàn)象

在列表中添加一條數(shù)據(jù),突然整個列表不顯示了

原因

之前將stickyHeaderIndices寫在state里了,其實不需要。。。

// Dva Model中effects的邏輯
let stickyHeaderIndices = [1];
if (!comments || comments.length <= 1) {
    stickyHeaderIndices = []; // error code
}
const {comments, stickyHeaderIndices} = this.props;

<FlatList
  ref={o => (this._list = o)}
  data={comments}
  ItemSeparatorComponent={this._separator}
  onEndReachedThreshold={0.01}
  onEndReached={this.onEndReached}
  ListHeaderComponent={this.renderDetail}
  renderItem={this.renderComments}
  ListFooterComponent={this.renderListFooter}
  keyExtractor={item => 'cmt_' + item.comment_id}
  stickyHeaderIndices={stickyHeaderIndices}
/>

當(dāng)stickyHeaderIndices從[] 變成 [1],整個FlatList就不顯示了.... (為找到這個問題,將代碼注到最少,來測試重現(xiàn),直到注掉stickyHeaderIndices發(fā)現(xiàn)正常了)

解決

第二行吸頂,直接設(shè)置 stickyHeaderIndices={[1]} 即可。

<FlatList
  ref={o => (this._list = o)}
  data={comments}
  ItemSeparatorComponent={this._separator}
  onEndReachedThreshold={0.01}
  onEndReached={this.onEndReached}
  ListHeaderComponent={this.renderDetail}
  renderItem={this.renderComments}
  ListFooterComponent={this.renderListFooter}
  keyExtractor={item => 'cmt_' + item.comment_id}
  stickyHeaderIndices={[1]}
/>

解決方法很簡單,但找出為什么突然白屏還是有點意思,記錄一下...


參考

https://docs.nativebase.io/docs/examples/FlatListExample.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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