React Native cloneWithRows 错误 - 对象未定义 [英] React Native cloneWithRows error - object undefined
问题描述
我对这个框架很陌生.我正在使用 React-Native 中的 dataSource 制作一个基本的 ListView,其中包含我获取的数据.
I'm quite new to the framework. I'm making a basic ListView with dataSource in React-Native, with data that I fetch.
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 响应中提取的:
the data I fetch is an extract from a twitter API response:
{ "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
}
]}
但是,我在 cloneWithRows 上收到undefined is not an object (evaluating 'dataSource.rowIdentities')"错误.当我在 iOS 8.4 和 9.1 上运行模拟时出现此错误.我可能遗漏了一些小东西并且被困了几个小时.有什么想法吗?
yet, I get a "undefined is not an object (evaluating 'dataSource.rowIdentities')" error on cloneWithRows. This error appears when I run the simulation on iOS 8.4 and 9.1. I'm probably missing something small and have been stuck for hours. Any ideas?
推荐答案
看代码,我猜你想在列表视图中显示状态.为此,将您的代码更改为 this.state.dataSource.cloneWithRows(responseData.statuses)
Looking at the code, I guess you want show the statuses inside listview. For that change your code to this.state.dataSource.cloneWithRows(responseData.statuses)
ListViewDataSource
源代码有详细的注释,解释了这个函数期望的格式 https://github.com/facebook/react-native/blob/62e8ddc20561a39c3c839ab9f83c95493df117c0/Libraries/CustomComponents/ListView/List1L5L一个>
ListViewDataSource
source code has detailed comment which explains the format this function is expecting https://github.com/facebook/react-native/blob/62e8ddc20561a39c3c839ab9f83c95493df117c0/Libraries/CustomComponents/ListView/ListViewDataSource.js#L95-L110
更新:
在 state 对象上设置 dataSource 属性,将构造函数中的代码更改为
Set the dataSource property on state object, change your code in the constructor to
<代码>this.state = {搜索字符串:'',isLoading: 假,信息: '',jsonData: '',数据源:数据源};
这篇关于React Native cloneWithRows 错误 - 对象未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!