React Native ListView row not re-rendering after state change

前端 未结 2 1439
独厮守ぢ
独厮守ぢ 2021-02-14 13:44

React Native ListView: Rows are not re-rendering after datasource state has changed.

Here is a simplified version of my code:



        
相关标签:
2条回答
  • 2021-02-14 14:05

    First you need to set the datasource in the getInitialState function. Then, change the datasource by calling this.setState({}) and passing in the new datasource. It looks like you may have been on the right track above, but I have set up a working example of changing the ListView datasource here . I hope this helps

    https://rnplay.org/apps/r3bzOQ

    0 讨论(0)
  • 2021-02-14 14:14

    react is smart enough to detect changes in dataSource and if the list should be re-rendered. If you want to update listView, create new objects instead of updating the properties of existing objects. The code would look something like this:

    let newArray = this._rows.slice();
    newArray[rowID] = {
      ...this._rows[rowID],
      newPropState: true,
    };
    this._rows = newArray;
    
    let newDataSource = this.ds.cloneWithRows(newArray);
    this.setState({
      dataSource: newDataSource
    });
    

    You can read more about similar issue on Github

    0 讨论(0)
提交回复
热议问题