[英]ListView.DataSource looping data for React Native
When I do a Console.log(this.state.dataSource) I get the image as below, I'm currently getting "Row goes here!"x 4 because the API return 4 results. 当我执行Console.log(this.state.dataSource)时,得到的图像如下,我当前得到的是“ Row go here!” x 4,因为API返回4结果。
I tried to retrieve the user data like email but it wont "loop" and print accordingly. 我试图检索用户数据(如电子邮件),但不会“循环”并进行相应打印。
I'm not exactly sure how this code works, comments would help abit.Please Advice 我不确定该代码的工作方式,注释会有所帮助。请咨询
var messagesTab = React.createClass({
componentWillMount: function() {
fetch('https://randomuser.me/api/?results=4')
.then(res => res.json())
.then(res => this.updateDataSource(res));
},
getInitialState: function() {
return {
dataSource: new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2
})
};
},
updateDataSource: function(data){
this.setState({
dataSource: this.state.dataSource.cloneWithRows(data)
})
},
renderRow: function (){
return (
<View>
<Text>Row goes here!</Text>
</View>
);
},
render: function(){
console.log(this.state.dataSource);
return (
<View style={ styleseact.container }>
<ListView dataSource={ this.state.dataSource } renderRow={ this.renderRow } />
</View>
);
}
});
I got this when I did a console.log(data); 当我做一个console.log(data);时,我得到了这个。 on renderRow
在renderRow上
You're overwriting your DataSource declaration. 您正在覆盖您的DataSource声明。 Split up the declaration and update.
拆分声明并更新。
Declare this before React.createClass
: 在
React.createClass
之前声明它:
var ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2
});
Remove it from getInitialState
: 从
getInitialState
删除它:
getInitialState: function() {
return {
dataSource: ds.cloneWithRows([])
};
},
And reference it in the update function: 并在更新函数中引用它:
updateDataSource: function(data){
this.setState({
dataSource: ds.cloneWithRows(data)
})
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.