react-nativereact-native-iosreact-native-listview

React Native slice() function undefined error


I want to update my listview row for that I am trying to slice listview datasource but its raising below error:

this.state.dataSource.slice is not a function. (In 'this.state.dataSource.slice()', 'this.state.dataSource.slice' is undefined)

And here is my code:

let newArray = this.state.dataSource.slice();
newArray[indexToUpdate] = {
  //Here I am updating my values.
};
this.setState({
  dataSource: this.state.dataSource.cloneWithRows(newArray),
});

Let me know if there is any error in my code or something wrong I have done.


Solution

  • this.state.dataSource is not an Array, that's why you cant do slice() on dataSource. It looks like this

    { _rowHasChanged: [Function: rowHasChanged],
      _getRowData: [Function: defaultGetRowData],
      _sectionHeaderHasChanged: [Function],
      _getSectionHeaderData: [Function: defaultGetSectionHeaderData],
      _dataBlob: { s1: [ 'row 1', 'row 2' ] },
      _dirtyRows: [ [ true, true ] ],
      _dirtySections: [ true ],
      _cachedRowCount: 2,
      rowIdentities: [ [ '0', '1' ] ],
      sectionIdentities: [ 's1' ] 
    }
    

    Your required array is in _dataBlob key of that object.

    Note: it's working fine and updates listview dataSource using below line

    this.state.dataSource._dataBlob.s1.slice()