first time ReactJS user and am having issues looping through data I retrieved via AJAX to populate. I get a number of errors ranging from Uncaught TypeError: Cannot read property 'map' of undefined. Am I supposed to be setting the props or a state?
var Posts = React.createClass({
getInitialState: function() {
return ({
posts: []
})
},
loadPosts: function(){
var posts = null;
axios.get('/getposts')
.then(function (result) {
posts = result.data;
})
.catch(function (error) {
console.log(error);
});
},
postReadMode: function() {
return (
<div>
</div>
)
},
render: function() {
return (
<div>
Hello, {this.props.posts.map(function(item, i){
return item.title;
})}
</div>
)
}
})
The data returned is somewhat of a multidimensional object/array:
Obj:
0:
title: 'Title 1',
date: '12-2-2015',
content: 'asdf'
1:
title: 'Title 2',
date: '1-25-2016',
content: 'asdfasdf'
setStatemethod. Try doing that, and it may solve your problem.this.setState({posts : result.data});inside the AJAX call, and then{this.state.posts.map(function(item, i){ return item.title})}with it returning a blank but I know theresult.datahave values in it