4

Here's my React class:

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;

It follows closely the code in tutorial16.js here (greater than halfway down the page).

However, the line

console.log(this.refs.noteToAdd.value);

prints undefined to the console. I'm not sure what I'm missing. The ref seems to be set up properly. The render code is very similar to that of the tutorial. But in the click handling function I cannot access the value of the input ref like in the tutorial. Why is this?

10
  • What version of React are you using? Commented Oct 8, 2015 at 15:38
  • Also, you don't have a click handler, you have a submit handler. Commented Oct 8, 2015 at 15:38
  • Try console.log(note) - I bet this works Commented Oct 8, 2015 at 15:40
  • 1
    @Mathletics React v0.12.2, changed title, thanks Commented Oct 8, 2015 at 15:40
  • @Titus No, it didn't, what's your thinking? They should produce the same thing since that's what I set note to. Commented Oct 8, 2015 at 15:43

2 Answers 2

6

In v0.12.0 use this.refs.noteToAdd.getDOMNode().value

In v0.13.0 use React.findDOMNode(this.refs.noteToAdd).value

In v0.14.0 use this.refs.noteTOAdd.value

Sign up to request clarification or add additional context in comments.

Comments

1

Try;

this.refs.noteToAdd.getDOMNode().value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.