我正在使用react来呈现从API中提取的数据。我的代码如下所示:无法读取未定义错误的属性'username'
var App = React.createClass({
getInitialState : function(){
return {
lists: []
}
},
componentDidMount: function(){
fetch('https://fcctop100.herokuapp.com/api/fccusers/top/recent')
.then(function(result){
return result.json();
}).then(function(jsonResult){
this.setState({
lists: jsonResult
});
}.bind(this));
},
render: function(){
console.log(this.state.lists);
return(
<div>{this.state.lists[0].username}</div>
);
}
});
ReactDOM.render(<App />, document.getElementById('container'));
我在渲染功能CONSOLE.LOG(this.state.lists),我从API得到了整个数据,但是当我呈现数据的一部分,我'不能读取属性'的'未定义'错误的用户名。如果我在getInitialState函数中设置了列表['']并且渲染{this.state.lists [0] .username},它可以工作,但是如果我将索引更改为1,我得到了同样的错误。我想这与生命周期功能有关。但我无法弄清楚。从API中获取的数据看起来像这样
我一直在为此工作3个小时。希望有人能帮助我。非常感激!
非常感谢!我得到它的工作!它非常有帮助。我更好地理解了组件生命周期!非常感谢! –
@博黄欢迎您。我建议您阅读https://facebook.github.io/react/docs/react-component.html以获得更好的理解。 – Ved