1
- /**
2
- * This file provided by Facebook is for non-commercial testing and evaluation
3
- * purposes only. Facebook reserves all rights not expressly granted.
4
- *
5
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
8
- * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
9
- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
10
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11
- */
1
+ // tutorial1.js
12
2
13
- var Comment = React . createClass ( {
14
- rawMarkup : function ( ) {
15
- var rawMarkup = marked ( this . props . children . toString ( ) , { sanitize : true } ) ;
16
- return { __html : rawMarkup } ;
17
- } ,
18
3
19
- render : function ( ) {
20
- return (
21
- < div className = "comment" >
22
- < h2 className = "commentAuthor" >
23
- { this . props . author }
24
- </ h2 >
25
- < span dangerouslySetInnerHTML = { this . rawMarkup ( ) } />
26
- </ div >
27
- ) ;
28
- }
29
- } ) ;
4
+ // we pass some methods in a javascript object to crate a new react component
5
+ // render returns a tree of react components that will eventually render to html
6
+
7
+ var data = [
8
+ { id : 1 , author : "Pete Hunt" , text : "This is one comment" } ,
9
+ { id : 2 , author : "Jordan Walke" , text : "This is *another* comment" }
10
+ ] ;
30
11
31
12
var CommentBox = React . createClass ( {
32
13
loadCommentsFromServer : function ( ) {
@@ -42,23 +23,6 @@ var CommentBox = React.createClass({
42
23
} . bind ( this )
43
24
} ) ;
44
25
} ,
45
- handleCommentSubmit : function ( comment ) {
46
- var comments = this . state . data ;
47
- var newComments = comments . concat ( [ comment ] ) ;
48
- this . setState ( { data : newComments } ) ;
49
- $ . ajax ( {
50
- url : this . props . url ,
51
- dataType : 'json' ,
52
- type : 'POST' ,
53
- data : comment ,
54
- success : function ( data ) {
55
- this . setState ( { data : data } ) ;
56
- } . bind ( this ) ,
57
- error : function ( xhr , status , err ) {
58
- console . error ( this . props . url , status , err . toString ( ) ) ;
59
- } . bind ( this )
60
- } ) ;
61
- } ,
62
26
getInitialState : function ( ) {
63
27
return { data : [ ] } ;
64
28
} ,
@@ -70,25 +34,24 @@ var CommentBox = React.createClass({
70
34
return (
71
35
< div className = "commentBox" >
72
36
< h1 > Comments</ h1 >
73
- < CommentList data = { this . state . data } />
74
- < CommentForm onCommentSubmit = { this . handleCommentSubmit } />
37
+ < CommentList data = { this . state . data } />
38
+ < CommentForm />
75
39
</ div >
76
40
) ;
77
41
}
78
42
} ) ;
79
43
80
44
var CommentList = React . createClass ( {
81
45
render : function ( ) {
82
- var commentNodes = this . props . data . map ( function ( comment , index ) {
46
+
47
+ var commentNodes = this . props . data . map ( function ( comment ) {
83
48
return (
84
- // `key` is a React-specific concept and is not mandatory for the
85
- // purpose of this tutorial. if you're curious, see more here:
86
- // http://facebook.github.io/react/docs/multiple-components.html#dynamic-children
87
- < Comment author = { comment . author } key = { index } >
49
+ < Comment author = { comment . author } key = { comment . id } >
88
50
{ comment . text }
89
51
</ Comment >
90
52
) ;
91
53
} ) ;
54
+
92
55
return (
93
56
< div className = "commentList" >
94
57
{ commentNodes }
@@ -97,29 +60,51 @@ var CommentList = React.createClass({
97
60
}
98
61
} ) ;
99
62
63
+ var Comment = React . createClass ( {
64
+ render : function ( ) {
65
+ return (
66
+ < div className = "comment" >
67
+ < h2 className = "commentAuthor" >
68
+ { this . props . author }
69
+ </ h2 >
70
+ { this . props . children }
71
+ </ div >
72
+ ) ;
73
+ }
74
+ } ) ;
75
+
100
76
var CommentForm = React . createClass ( {
101
- handleSubmit : function ( e ) {
102
- e . preventDefault ( ) ;
103
- var author = this . refs . author . value . trim ( ) ;
104
- var text = this . refs . text . value . trim ( ) ;
105
- if ( ! text || ! author ) {
106
- return ;
107
- }
108
- this . props . onCommentSubmit ( { author : author , text : text } ) ;
109
- this . refs . author . value = '' ;
110
- this . refs . text . value = '' ;
77
+ getInitialState : function ( ) {
78
+ return { author : '' , text : '' } ;
79
+ } ,
80
+ handleAuthorChange : function ( e ) {
81
+ this . setState ( { author : e . target . value } ) ;
82
+ } ,
83
+ handleTextChange : function ( e ) {
84
+ this . setState ( { text : e . target . value } ) ;
111
85
} ,
112
86
render : function ( ) {
113
87
return (
114
- < form className = "commentForm" onSubmit = { this . handleSubmit } >
115
- < input type = "text" placeholder = "Your name" ref = "author" />
116
- < input type = "text" placeholder = "Say something..." ref = "text" />
117
- < input type = "submit" value = "Post" />
88
+ < form className = "commentForm" >
89
+ < input
90
+ type = "text"
91
+ placeholder = "Your name"
92
+ value = { this . state . author }
93
+ onChange = { this . handleAuthorChange }
94
+ />
95
+ < input
96
+ type = "text"
97
+ placeholder = "Say something..."
98
+ value = { this . state . text }
99
+ onChange = { this . handleTextChange }
100
+ />
101
+ < input type = "submit" value = "Post" />
118
102
</ form >
119
103
) ;
120
104
}
121
105
} ) ;
122
106
107
+ // injects the markup into a dom element, supplied as the second argument
123
108
ReactDOM . render (
124
109
< CommentBox url = "/api/comments" pollInterval = { 2000 } /> ,
125
110
document . getElementById ( 'content' )
0 commit comments