@@ -100,22 +100,40 @@ var CommentList = React.createClass({
100
100
} ) ;
101
101
102
102
var CommentForm = React . createClass ( {
103
+ getInitialState : function ( ) {
104
+ return { author : '' , text : '' } ;
105
+ } ,
106
+ handleAuthorChange : function ( e ) {
107
+ this . setState ( { author : e . target . value } ) ;
108
+ } ,
109
+ handleTextChange : function ( e ) {
110
+ this . setState ( { text : e . target . value } ) ;
111
+ } ,
103
112
handleSubmit : function ( e ) {
104
113
e . preventDefault ( ) ;
105
- var author = this . refs . author . value . trim ( ) ;
106
- var text = this . refs . text . value . trim ( ) ;
114
+ var author = this . state . author . trim ( ) ;
115
+ var text = this . state . text . trim ( ) ;
107
116
if ( ! text || ! author ) {
108
117
return ;
109
118
}
110
119
this . props . onCommentSubmit ( { author : author , text : text } ) ;
111
- this . refs . author . value = '' ;
112
- this . refs . text . value = '' ;
120
+ this . setState ( { author : '' , text : '' } ) ;
113
121
} ,
114
122
render : function ( ) {
115
123
return (
116
124
< form className = "commentForm" onSubmit = { this . handleSubmit } >
117
- < input type = "text" placeholder = "Your name" ref = "author" />
118
- < input type = "text" placeholder = "Say something..." ref = "text" />
125
+ < input
126
+ type = "text"
127
+ placeholder = "Your name"
128
+ value = { this . state . author }
129
+ onChange = { this . handleAuthorChange }
130
+ />
131
+ < input
132
+ type = "text"
133
+ placeholder = "Say something..."
134
+ value = { this . state . text }
135
+ onChange = { this . handleTextChange }
136
+ />
119
137
< input type = "submit" value = "Post" />
120
138
</ form >
121
139
) ;
0 commit comments