React Native slice() 函数未定义错误

React Native slice() function undefined error

我想更新我的 listview 行,因为我正在尝试切片 listview 数据源,但它引发了以下错误:

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

这是我的代码:

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

如果我的代码有任何错误或我做错了什么,请告诉我。

this.state.dataSource 不是数组,这就是为什么不能对数据源执行 slice() 的原因。看起来像这样

{ _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' ] 
}

您需要的数组在该对象的 _dataBlob 键中。

注意:它工作正常并使用下面的行更新列表视图数据源

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