React不在提交处理程序中查找<input />值

Sco*_*y H 4 javascript forms reactive-programming reactjs

这是我的React类:

var React = require('bower.js').React;
var paper = require('bower.js').paper;

var NotePanel = React.createClass({
  getInitialState: function() {
    return {
      noteToAdd: ""
    };
  },
  setNoteToAdd: function(event) {
    event.preventDefault();
    var note = this.refs.noteToAdd.value;
    console.log(this.refs.noteToAdd.value);
    this.setState({
        noteToAdd: note
    });
  },
  render: function() {
    return (
      <div>
        <form onSubmit={this.setNoteToAdd}>
          <input ref="noteToAdd" type="text" />
          <input type="submit" value="Add Note" />
        </form>
      </div>
    );
  }
});

module.exports = NotePanel;
Run Code Online (Sandbox Code Playgroud)

它紧跟在tutorial16.js 这里的代码(大于页面的一半).

但是,行

console.log(this.refs.noteToAdd.value);
Run Code Online (Sandbox Code Playgroud)

将undefined打印到控制台.我不确定我错过了什么.ref似乎设置正确.渲染代码与教程非常相似.但是在点击处理功能中我无法访问教程value中的input ref类似功能.为什么是这样?

Pra*_*Raj 6

v0.12.0中使用this.refs.noteToAdd.getDOMNode().value

v0.13.0中使用React.findDOMNode(this.refs.noteToAdd).value

v0.14.0中使用this.refs.noteTOAdd.value