新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)React教程:ReactAJAX
React 組件的數(shù)據(jù)可以通過 componentDidMount 方法中的 Ajax 來獲取,當(dāng)從服務(wù)端獲取數(shù)據(jù)庫可以將數(shù)據(jù)存儲在 state 中,再用 this.setState 方法重新渲染 UI。

創(chuàng)新互聯(lián)云計算的互聯(lián)網(wǎng)服務(wù)提供商,擁有超過13年的服務(wù)器租用、BGP機房服務(wù)器托管、云服務(wù)器、網(wǎng)站空間、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗,已先后獲得國家工業(yè)和信息化部頒發(fā)的互聯(lián)網(wǎng)數(shù)據(jù)中心業(yè)務(wù)許可證。專業(yè)提供云主機、網(wǎng)站空間、申請域名、VPS主機、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
當(dāng)使用異步加載數(shù)據(jù)時,在組件卸載前使用 componentWillUnmount 來取消未完成的請求。
以下實例演示了獲取 Github 用戶最新 gist 共享描述:
class UserGist extends React.Component {
constructor(props) {
super(props);
this.state = {username: '', lastGistUrl: ''};
}
componentDidMount() {
this.serverRequest = $.get(this.props.source, function (result) {
var lastGist = result[0];
this.setState({
username: lastGist.owner.login,
lastGistUrl: lastGist.html_url
});
}.bind(this));
}
componentWillUnmount() {
this.serverRequest.abort();
}
render() {
return (
{this.state.username} 用戶最新的 Gist 共享地址:
{this.state.lastGistUrl}
);
}
}
ReactDOM.render(
,
document.getElementById('example')
);
嘗試一下 ?
以上代碼使用 jQuery 完成 Ajax 請求。
相關(guān)教程
AJAX教程
文章名稱:創(chuàng)新互聯(lián)React教程:ReactAJAX
文章分享:http://m.fisionsoft.com.cn/article/dhhecic.html


咨詢
建站咨詢
