React Native cloneWithRows错误 – 对象未定义

我对这个框架很陌生。 我正在使用React-Native中的dataSource制作一个基本的ListView,并提取数据。

constructor(props){ super(props); var dataSource = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2, }); this.state = { searchString: '', isLoading: false, message: '', jsonData: '', }; } _fetchData(query){ console.log(query); this.setState({ isLoading: true }); fetch(query) .then(response => response.json()) .then(responseData => { this._handleResponse(responseData); this.setState({ message: 'seems like it worked!', dataSource: this.state.dataSource.cloneWithRows( responseData ), }); }) .catch(error => this.setState({ isLoading: false, message: this.state.message + ' --- ' + 'Something bad happened' + error }) ).done(); } 

我获取的数据是来自twitter API响应的摘录:

 { "statuses": [ { "coordinates": null, "favorited": false, "truncated": false, "created_at": "Mon Sep 24 03:35:21 +0000 2012", "id_str": "250075927172759552", "retweet_count": 0, "in_reply_to_status_id_str": null, "id": 250075927172759552 } ]} 

然而,我得到一个“未定义不是一个对象(评估'dataSource.rowIdentities')”cloneWithRows错误。 我在iOS 8.4和9.1上运行模拟时出现此错误。 我可能错过了一些小小的东西,而且一直停留几个小时。 有任何想法吗?

看代码,我想你想显示列表视图内的状态。 为此将您的代码更改为this.state.dataSource.cloneWithRows(responseData.statuses)

ListViewDataSource源代码有详细的评论,这解释了这个函数期待的格式https://github.com/facebook/react-native/blob/62e8ddc20561a39c3c839ab9f83c95493df117c0/Libraries/CustomComponents/ListView/ListViewDataSource.js#L95-L110

更新:

设置状态对象的dataSource属性,在构造函数中更改你的代码

this.state = { searchString: '', isLoading: false, message: '', jsonData: '', dataSource: dataSource };