The example here - https://facebook.github.io/react/tips/initial-ajax.html - is modified below.
this.state.username
is "placeholder" on load and is passed to <UserGist />
After AJAX loads this.state.username
is changed to "octocat", but...
How come this is not passed down to the <UserGist />
component?
var UserGist = React.createClass({
getInitialState: function() {
return {
username: this.props.username
};
},
render: function() {
return (
<div>
{this.state.username}
</div>
);
}
});
var App = React.createClass({
getInitialState: function() {
return {
username: 'placeholder'
};
},
componentDidMount: function() {
this.serverRequest = $.get(this.props.source, function (result) {
var lastGist = result[0];
this.setState({
username: lastGist.owner.login
});
}.bind(this));
},
componentWillUnmount: function() {
this.serverRequest.abort();
},
render: function() {
return (
<div>
<UserGist username={this.state.username} />
</div>
);
}
});
ReactDOM.render(
<App source="https://api.github.com/users/octocat/gists" />,
document.getElementById('main')
);