Skip to content

Commit 1829d6c

Browse files
author
James Robson
committed
Switched to using state
1 parent 288bafe commit 1829d6c

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"globals": {
77
"React": 0,
88
"Remarkable": 0,
9-
"ReactDOM": 0
9+
"ReactDOM": 0,
10+
"$": 0
1011
},
1112
"extends": "eslint:recommended",
1213
"parserOptions": {

public/scripts/tutorial.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,34 @@
2424
});
2525

2626
let CommentBox = React.createClass({
27+
loadCommentsFromServer: function() {
28+
$.ajax({
29+
url: this.props.url,
30+
dataType: 'json',
31+
cache: false,
32+
success: function(data) {
33+
this.setState({data: data});
34+
}.bind(this),
35+
error: function(xhr, status, err) {
36+
console.error(this.props.url, status, err.toString());
37+
}.bind(this)
38+
});
39+
},
40+
41+
getInitialState: function() {
42+
return {data: []};
43+
},
44+
45+
componentDidMount: function() {
46+
this.loadCommentsFromServer();
47+
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
48+
},
49+
2750
render: function() {
2851
return (
2952
<div className="commentBox">
3053
<h1>Comments</h1>
31-
<CommentList data={this.props.data} />
54+
<CommentList data={this.state.data} />
3255
<CommentForm />
3356
</div>
3457
);
@@ -62,7 +85,7 @@
6285
}
6386
});
6487
ReactDOM.render(
65-
<CommentBox data={data} />,
88+
<CommentBox url="/api/comments" pollInterval={2000}/>,
6689
document.getElementById('content')
6790
);
6891
})();

0 commit comments

Comments
 (0)