Skip to content

Instantly share code, notes, and snippets.

@techman83
Created August 31, 2015 10:20
Show Gist options
  • Save techman83/e0afa285e5e96a4ae906 to your computer and use it in GitHub Desktop.
Save techman83/e0afa285e5e96a4ae906 to your computer and use it in GitHub Desktop.
React JS learnings
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello React</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<div id="content"></div>
<script type="text/jsx">
var CommentBox = React.createClass({
loadCommentsFromServer: function() {
$.ajax({
url: this.props.url,
dataType: 'json',
cache: false,
success: function(data) {
var netkan = Object.keys(data).map(function(key) {
var item = data[key];
item.id = key;
return item;
});
this.setState({data: netkan});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
getInitialState: function() {
return {data: []};
},
componentDidMount: function() {
this.loadCommentsFromServer();
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
},
render: function() {
return (
<div className="commentBox">
<h1>Comments</h1>
<CommentList data={this.state.data} />
</div>
);
}
});
var CommentList = React.createClass({
render: function() {
this.props.data.map(function (comment) { console.log(comment) } );
var commentNodes = this.props.data.map(function (comment) {
return (
<Comment id={comment.id}>
{comment.last_checked}
</Comment>
);
});
console.log(commentNodes);
return (
<div className="commentList">
{commentNodes}
</div>
);
}
});
React.render(
<CommentBox url="https://dl.dropboxusercontent.com/u/8415802/test.json" pollInterval={300000} />,
document.getElementById('content')
);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment