鍍金池/ 教程/ HTML/ 通過(guò) AJAX 加載初始數(shù)據(jù)
顯示數(shù)據(jù)
組件的引用
Controlled Input 值為 null 的情況
Reconciliation
子 props 的類(lèi)型
組件的詳細(xì)說(shuō)明和生命周期
傳遞 Props
特殊的非 DOM 屬性
組件 API
PureRenderMixin
雙向綁定輔助工具
瀏覽器中的工作原理
深入 JSX
表單組件
Dangerously Set innerHTML
入門(mén)
JSX 中的 If-Else
克隆組件
教程
更多的關(guān)于Refs
JSX 的 false 處理
高級(jí)性能
Mounting 后 componentWillReceiveProps 未被觸發(fā)
簡(jiǎn)介
測(cè)試工具集
JSX 陷阱
工具集成(ToolingIntegration)
公開(kāi)組件功能
通過(guò) AJAX 加載初始數(shù)據(jù)
事件系統(tǒng)
可復(fù)用組件
this.props.children undefined
不可變數(shù)據(jù)的輔助工具(Immutability Helpers)
動(dòng)態(tài)交互式用戶界面
組件的 DOM 事件監(jiān)聽(tīng)
復(fù)合組件
動(dòng)畫(huà)
插件
JSX 展開(kāi)屬性
行內(nèi)樣式
性能分析工具
類(lèi)名操作
與其他類(lèi)庫(kù)并行使用 React
鍵控的片段
標(biāo)簽和屬性支持
組件間的通信
React (虛擬)DOM 術(shù)語(yǔ)
JSX 根節(jié)點(diǎn)的最大數(shù)量
在樣式props中快速制定像素值
頂層 API
深入理解 React
自閉合標(biāo)簽
為什么使用 React?
getInitialState 里的 Props 是一個(gè)反模式
與 DOM 的差異

通過(guò) AJAX 加載初始數(shù)據(jù)

componentDidMount 時(shí)加載數(shù)據(jù)。當(dāng)加載成功,將數(shù)據(jù)存儲(chǔ)在 state 中,觸發(fā) render 來(lái)更新你的 UI。

當(dāng)執(zhí)行同步請(qǐng)求的響應(yīng)時(shí),在更新 state 前, 一定要先通過(guò) this.isMounted() 來(lái)檢測(cè)組件的狀態(tài)是否還是 mounted。

下面這個(gè)例子請(qǐng)求了一個(gè) Github 用戶最近的 gist:

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

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

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

React.render(
  <UserGist source="https://api.github.com/users/octocat/gists" />,
  mountNode
);