I'm building an RSS reader based upon the CommentBox example on the react website.
When attempting to save the results from the google feed API to state, the state is not being succesfully saved outside of the jQuery ajax function, where I can read out the variable to console.log and the resulting information is correct but when I read out the state outside of the function it's printing out the empty array this is initiated when the component is first called.
var RSSApp = React.createClass({
getInitialState: function () {
return {feed:[]};
},
componentDidMount: function () {
this.loadRssFeed();
},
loadRssFeed: function() {
$.ajax({
url: 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=-1&q=' + encodeURIComponent(this.props.url),
dataType: "jsonp",
jsonCallback: "reponseData",
success: function(feed) {
var feeds = feed.responseData.feed.entries;
console.log(feeds)
console.log(feed.responseData.feed)
this.setState({feeds:feed});
}.bind(this)
});
console.log(this.state)
},
render: function () {
return (
<div className="RSSBox">
<h1>RSS Feed built using ReactJS</h1>
<RSSForm />
<RSSList data={this.state.feed} />
</div>
)
}
});
Where am I going wrong in saving the state?
console.logdoes not work for you?