1
- var CommentBox = React . createClass ( {
2
- loadCommentsFromServer : function ( ) {
1
+ class CommentBox extends React . Component {
2
+ state = { data : this . props . initialData } ;
3
+
4
+ loadCommentsFromServer = ( ) => {
3
5
var xhr = new XMLHttpRequest ( ) ;
4
6
xhr . open ( 'get' , this . props . url , true ) ;
5
- xhr . onload = function ( ) {
7
+ xhr . onload = function ( ) {
6
8
var data = JSON . parse ( xhr . responseText ) ;
7
9
this . setState ( { data : data } ) ;
8
10
} . bind ( this ) ;
9
11
xhr . send ( ) ;
10
- } ,
11
- handleCommentSubmit : function ( comment ) {
12
+ } ;
13
+
14
+ handleCommentSubmit = comment => {
12
15
var comments = this . state . data ;
13
16
// Optimistically set an id on the new comment. It will be replaced by an
14
17
// id generated by the server. In a production application you would likely
23
26
24
27
var xhr = new XMLHttpRequest ( ) ;
25
28
xhr . open ( 'post' , this . props . submitUrl , true ) ;
26
- xhr . onload = function ( ) {
29
+ xhr . onload = function ( ) {
27
30
this . loadCommentsFromServer ( ) ;
28
31
} . bind ( this ) ;
29
32
xhr . send ( data ) ;
30
- } ,
31
- getInitialState : function ( ) {
32
- return { data : this . props . initialData } ;
33
- } ,
34
- componentDidMount : function ( ) {
33
+ } ;
34
+
35
+ componentDidMount ( ) {
35
36
window . setInterval ( this . loadCommentsFromServer , this . props . pollInterval ) ;
36
- } ,
37
- render : function ( ) {
37
+ }
38
+
39
+ render ( ) {
38
40
return (
39
41
< div className = "commentBox" >
40
42
< h1 > Comments</ h1 >
41
43
< CommentList data = { this . state . data } />
42
- < CommentForm onCommentSubmit = { this . handleCommentSubmit } />
44
+ < CommentForm onCommentSubmit = { this . handleCommentSubmit } />
43
45
</ div >
44
- ) ;
46
+ ) ;
45
47
}
46
- } ) ;
48
+ }
47
49
48
- var CommentList = React . createClass ( {
49
- render : function ( ) {
50
+ class CommentList extends React . Component {
51
+ render ( ) {
50
52
var commentNodes = this . props . data . map ( function ( comment ) {
51
53
return (
52
- < Comment author = { comment . author } key = { comment . id } >
53
- { comment . text }
54
+ < Comment author = { comment . author } key = { comment . id } >
55
+ { comment . text }
54
56
</ Comment >
55
- ) ;
57
+ ) ;
56
58
} ) ;
57
- return (
58
- < div className = "commentList" >
59
- { commentNodes }
60
- </ div >
61
- ) ;
59
+ return < div className = "commentList" > { commentNodes } </ div > ;
62
60
}
63
- } ) ;
64
-
65
- var CommentForm = React . createClass ( {
66
- getInitialState : function ( ) {
67
- return { author : '' , text : '' } ;
68
- } ,
69
- handleAuthorChange : function ( e ) {
70
- this . setState ( { author : e . target . value } ) ;
71
- } ,
72
- handleTextChange : function ( e ) {
73
- this . setState ( { text : e . target . value } ) ;
74
- } ,
75
- handleSubmit : function ( e ) {
61
+ }
62
+
63
+ class CommentForm extends React . Component {
64
+ state = {
65
+ author : '' ,
66
+ text : ''
67
+ } ;
68
+
69
+ handleAuthorChange = e => {
70
+ this . setState ( { author : e . target . value } ) ;
71
+ } ;
72
+
73
+ handleTextChange = e => {
74
+ this . setState ( { text : e . target . value } ) ;
75
+ } ;
76
+
77
+ handleSubmit = e => {
76
78
e . preventDefault ( ) ;
77
79
var author = this . state . author . trim ( ) ;
78
80
var text = this . state . text . trim ( ) ;
@@ -81,8 +83,9 @@ var CommentForm = React.createClass({
81
83
}
82
84
this . props . onCommentSubmit ( { author : author , text : text } ) ;
83
85
this . setState ( { author : '' , text : '' } ) ;
84
- } ,
85
- render : function ( ) {
86
+ } ;
87
+
88
+ render ( ) {
86
89
return (
87
90
< form className = "commentForm" onSubmit = { this . handleSubmit } >
88
91
< input
@@ -101,27 +104,30 @@ var CommentForm = React.createClass({
101
104
</ form >
102
105
) ;
103
106
}
104
- } ) ;
107
+ }
105
108
106
109
function createRemarkable ( ) {
107
- var remarkable = ( ( "undefined" != typeof global ) && ( global . Remarkable ) ) ? global . Remarkable : window . Remarkable ;
108
- return new remarkable ( ) ;
110
+ var remarkable =
111
+ 'undefined' != typeof global && global . Remarkable
112
+ ? global . Remarkable
113
+ : window . Remarkable ;
114
+
115
+ return new remarkable ( ) ;
109
116
}
110
117
111
- var Comment = React . createClass ( {
112
- rawMarkup : function ( ) {
118
+ class Comment extends React . Component {
119
+ rawMarkup = ( ) => {
113
120
var md = createRemarkable ( ) ;
114
121
var rawMarkup = md . render ( this . props . children . toString ( ) ) ;
115
122
return { __html : rawMarkup } ;
116
- } ,
117
- render : function ( ) {
123
+ } ;
124
+
125
+ render ( ) {
118
126
return (
119
- < div className = "comment" >
120
- < h2 className = "commentAuthor" >
121
- { this . props . author }
122
- </ h2 >
123
- < span dangerouslySetInnerHTML = { this . rawMarkup ( ) } />
124
- </ div >
125
- ) ;
127
+ < div className = "comment" >
128
+ < h2 className = "commentAuthor" > { this . props . author } </ h2 >
129
+ < span dangerouslySetInnerHTML = { this . rawMarkup ( ) } />
130
+ </ div >
131
+ ) ;
126
132
}
127
- } ) ;
133
+ }
0 commit comments