소스 검색

Listing 16: adding comments - controlled components.

Frederic G. MARAND 8 년 전
부모
커밋
1c3a801ac8
1개의 변경된 파일24개의 추가작업 그리고 2개의 파일을 삭제
  1. 24 2
      public/scripts/tutorial.js

+ 24 - 2
public/scripts/tutorial.js

@@ -33,12 +33,34 @@ let CommentList = React.createClass({
 });
 
 let CommentForm = React.createClass({
+  getInitialState: function () {
+    return {
+      author: "",
+      text: ""
+    };
+  },
+  handleAuthorChange: function (e) {
+    this.setState({ author: e.target.value });
+  },
+  handleTextChange: function (e) {
+    this.setState({ text: e.target.value });
+  },
   render: function () {
     return (
       <div className="commentForm">
         <form className="commentForm">
-          <input type="text" placeholder="Your name" />
-          <input type="text" placeholder="Say something..." />
+          <input
+            type="text"
+            placeholder="Your name"
+            value={this.state.author}
+            onChange={this.handleAuthorChange}
+          />
+          <input
+            type="text"
+            placeholder="Say something..."
+            value={this.state.text}
+            onChange={this.handleTextChange}
+          />
           <input type="submit" value="Post" />
         </form>
       </div>