I have a series of React components loading data from HTTP service in componentDidMount as explained here http://facebook.github.io/react/tips/initial-ajax.html
<html>
<body>
<script src="http://fb.me/react-0.13.0.min.js"></script>
<script src="http://fb.me/JSXTransformer-0.13.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/jsx">
var RandomGist = React.createClass({
getInitialState: function() {
return {
id: '',
url: ''
};
},
componentDidMount: function() {
$.get(this.props.source, function(result) {
var gist = result[Math.round(Math.random() * result.length)];
this.setState({
id: gist.id,
url: gist.html_url
});
}.bind(this));
},
render: function() {
return (
<div>
<a href={this.state.url}>{this.state.id}</a>
</div>
);
}
});
var GistList = React.createClass({
render: function() {
return (<div>{[1,2,3].map(function() {
return <RandomGist source="https://api.github.com/users/octocat/gists" />;
})}</div>);
}
});
var RefreshableGistList = React.createClass({
handleClick: function() {
console.log("Click!");
},
render: function() {
return (
<div>
<button onClick={this.handleClick}>Refresh</button>
<GistList />
</div>
);
}
});
React.render(
<RefreshableGistList />,
document.body
);
</script>
</body>
</html>
After click on Refresh button I would like to refresh gist from HTTP service. Problem is that setState and forceUpdate will not lead to that because getting data from HTTP service is done in componentDidMount as was explained in original react example.