소스 검색

Step 4.2: Add handleSubmit method to App component.

Frederic G. MARAND 9 년 전
부모
커밋
fbef3e9528
1개의 변경된 파일18개의 추가작업 그리고 1개의 파일을 삭제
  1. 18 1
      App.jsx

+ 18 - 1
App.jsx

@@ -21,6 +21,22 @@ App = React.createClass({
     });
   },
 
+  handleSubmit(event) {
+    event.preventDefault();
+    // Meteor._debug('refs', this.refs);
+
+    // Find the text field via the React ref.
+    var text = ReactDOM.findDOMNode(this.refs.textInput).value.trim();
+
+    Tasks.insert({
+      text: text,
+      createdAt: new Date() // Current time
+    });
+
+    // Clear form to allow a new input.
+    ReactDOM.findDOMNode(this.refs.textInput).value = '';
+  },
+
   render() {
     return (
       <div className="container">
@@ -28,7 +44,8 @@ App = React.createClass({
           <h1>Todo list</h1>
 
           {/* These are JSX comments. */}
-          <form className="new-task" onsubmit={this.handleSubmit} >
+          {/* Beware: for React, onsubmit is not the same as onSubmit, the former doesn't work */}
+          <form className="new-task" onSubmit={this.handleSubmit} >
             <input type="text" ref="textInput" placeholder="Type to add new tasks" />
             </form>
           </header>