0

Was recently building similar component to facebook's comment tutorial, so I've picked the same code and realized it lacks callback-based input clearing. So what's the best way to wire that into react/flux architecture?

So _id & name inputs are being cleared independent to success of the form submit.

  handleSubmit: function(e) {
    e.preventDefault();
    var _id = this.refs._id.getDOMNode().value.trim();
    var name = this.refs.name.getDOMNode().value.trim();
    if (!_id || !name) {
      return;
    }
    this.props.onCommentSubmit({_id: _id, name: name});
    this.refs._id.getDOMNode().value = '';
    this.refs.name.getDOMNode().value = '';
  },
Leg0
  • 510
  • 9
  • 21

1 Answers1

0

You should not need to manipulate DOM nodes with React.

Why don't you try using state?

getDefaultState: function() {
  return {
    _id: '',
    name: ''
  };
},

handleSubmit: function() {
  if (!this.state._id || !this.state.name) {
    return;
  }
  this.props.onCommentSubmit({
    _id: this.state._id,
    name: this.state.name
  });
  this.setState({
    _id: '',
    name: ''
  });
},

onNameChange: function(value) {
  this.setState({ name: value});
},

onIdChange: function(value) {
  this.setState({ _id: value });
},

render: function() {
  return (
    <div>
      <input defaultValue={this.state._id} onChange={this.onIdChange} />
      <input defaultValue={this.state.name} onChange={this.onNameChange} />
    </div>
  );
}
Wiktor Kozlik
  • 783
  • 1
  • 6
  • 7
  • Sorry but the question is not about how to manipulate state, but rather how to asses async changes of state along components. – Leg0 Feb 22 '15 at 19:36