Load Initial Data via AJAX

Fetch data in componentDidMount. When the response arrives, store the data in state, triggering a render to update your UI.

When fetching data asynchronously, use componentWillUnmount to cancel any outstanding requests before the component is unmounted.

This example fetches the desired Github user's latest gist:

var UserGist = React.createClass({
  getInitialState: function() {
    return {
      username: '',
      lastGistUrl: ''

  componentDidMount: function() {
    this.serverRequest = $.get(this.props.source, function (result) {
      var lastGist = result[0];
        username: lastGist.owner.login,
        lastGistUrl: lastGist.html_url

  componentWillUnmount: function() {

  render: function() {
    return (
        {this.state.username}'s last gist is
        <a href={this.state.lastGistUrl}>here</a>.

  <UserGist source="https://api.github.com/users/octocat/gists" />,

results matching ""

    No results matching ""