反应本机slice()函数未定义错误

Ani*_*ake 1 react-native react-native-listview react-native-ios

我想更新我的列表视图行,因为我正尝试对列表视图数据源进行切片,但其引发以下错误:

this.state.dataSource.slice不是函数。(在“ this.state.dataSource.slice()”中,“ this.state.dataSource.slice”未定义)

这是我的代码:

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

让我知道代码中是否有错误或做错了什么。

Har*_*iks 5

this.state.dataSource不是数组,这就是为什么您不能slice()在dataSource上做的原因。看起来像这样

{ _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' ] 
}
Run Code Online (Sandbox Code Playgroud)

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

注意:它工作正常,并使用下面的行更新listview dataSource

this.state.dataSource._dataBlob.s1.slice()
Run Code Online (Sandbox Code Playgroud)