1. 程式人生 > >React Native FlatList強制重新整理Changing numColumns on the fly is not supported

React Native FlatList強制重新整理Changing numColumns on the fly is not supported

紅色的喜慶顏色又來了。

這個報錯很明白,就是不讓你重新整理,因為你的key沒變,我猜他重新整理的判斷key沒變所以判斷資料沒變無需重新整理,但是實際上資料確實變了,那我們就把key變一下把

_renderItem = (info) => {
    let numColumns = 2;
    const { screenMode } = this.props;
    switch (screenMode) {
      case 'Grid':
        numColumns = 2;
        break;
      case 'List':
        numColumns = info.section.data[0
].length; break; default: } return( <FlatList data = {info.section.data[0]} numColumns = {numColumns} horizontal={false} renderItem = {this.renderItem} keyExtractor ={this._extraUniqueKey} key = {screenMode} /> ) }

多了的就是FlatList中的key,我這邊的是Reducer的props來改動的,如果你沒用到就設定個map自己手動改變一下key,比如按鈕點選改動map的物件就可以了。