React Ajax

<div id="example"></div>
<script type="text/babel">
var Ajaxs=React.createClass({
getInitialState:function(){
return{
username:'',
lastGistUrl:''
};
},
componentDidMount:function(){
/* 通过Ajax获取组件中的数据*/
this.serverRequest= $.get(this.props.source,function(result){
var lastGist=result[0];
this.setState({
lastGistUrl:lastGist.html_url
});
}.bind(this))
},
componentWillUnmount:function(){
/*异常退出*/
this.serverRequest.abort();
},
render:function(){
return(
<div>
用户最新的Gist共享地址:
<a href={this.state.lastGistUrl}>{this.state.lastGistUrl}</a>
</div>
)
}

});
ReactDOM.render(
<Ajaxs source="https://api.github.com/users/octocat/gists"/>,
document.getElementById('example')
);
</script>
posted @ 2018-01-05 14:03  yaomengli  阅读(131)  评论(0编辑  收藏  举报