React-Native Updating List View DataSource

后端 未结 3 1936
借酒劲吻你
借酒劲吻你 2020-12-02 15:32

I have an iOS app I am making with react-native. The Game class contains a ListView component. I set the state in the constructor and include a dataSource. I ha

3条回答
  •  失恋的感觉
    2020-12-02 16:07

    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],
      Selection: !this._rows[rowID].Selection,
    };
    this._rows = newArray;
    
    let newDataSource = this.ds.cloneWithRows(newArray);
    this.setState({
      dataSource: newDataSource
    });
    

    You can read more about similar issue on Github

提交回复
热议问题