Last active
November 9, 2017 09:08
-
-
Save kenzhaoyihui/b29cf274c4d57aeef44bc7dad4cb4110 to your computer and use it in GitHub Desktop.
React Ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<title> React </title> | |
<script src="https://cdn.bootcss.com/react/15.4.2/react.min.js"></script> | |
<script src="https://cdn.bootcss.com/react/15.4.2/react-dom.min.js"></script> | |
<script src="https://cdn.bootcss.com/babel-standalone/6.22.1/babel.min.js"></script> | |
<script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script> | |
</head> | |
<body> | |
<div id="example"></div> | |
<script type="text/babel"> | |
var UserGist = React.createClass({ | |
getInitialState: function() { | |
return { | |
username: '', | |
lastGistUrl: '' | |
}; | |
}, | |
componentDidMount: function() { | |
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: function() { | |
this.serverRequest.abort(); | |
}, | |
render: function() { | |
return ( | |
<div> | |
{this.state.username} 用户最新的 Gist 共享地址: | |
<a href={this.state.lastGistUrl}>{this.state.lastGistUrl}</a> | |
</div> | |
); | |
} | |
}); | |
ReactDOM.render( | |
<UserGist source="https://api.github.com/users/octocat/gists" />, | |
document.getElementById('example') | |
); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment