提交后清除 reactJs textarea

Clear reactJs textarea after submit

我在天窗对话框中有以下表单组件,提交后,如果重新打开包含表单的对话框,它会包含之前提交的值。谁能告诉我如何在每次打开对话框时停止它并清除文本区域值?

这是我的组件:

var AddNoteForm = React.createClass({


componentDidMount: function() {

        React.findDOMNode(this.refs.notes).value = "";
},
handleSubmit: function (event) {
    event.preventDefault();

    var notes = React.findDOMNode(this.refs.notes).value;

    var details = {
        studentId: this.props.studentId,
        schoolId: this.props.schoolId,
        notes: notes
    };

    this.props.onSubmit(details);
},

render: function() {
    return (
        <form className="pure-form pure-form-aligned"
            onSubmit={this.handleSubmit}>
            <div className="pure-control-group">
                <label htmlFor="notes">Note</label>
                <textarea ref="notes" id="notes" placeholder="Note..." >
                </textarea>

            </div>
            <div className="pure-controls">
                <input type="submit" value="Save" />
            </div>
        </form>
    );
}
});

module.exports = AddNoteForm;

基本上您的表单没有被卸载。所以把代码写在 componentDidMount 里就没有意义了。因此,快速解决问题的方法是在读取句柄提交方法

中的值后清除文本框
handleSubmit: function (event) {
  event.preventDefault();

  var notes = this.refs.notes;

  var details = {
    studentId: this.props.studentId,
    schoolId: this.props.schoolId,
    notes: notes.value
  };

  notes.value = ""; // Unset the value
  this.props.onSubmit(details);
},

所以如果有人遇到这个问题, 我正在使用不受控制的组件,清除它有点复杂, 刚换成受控的就搞定了:)

<form onSubmit={e => this.handleSubmit(e)}>
<textarea value={this.state.text} onChange={ e => this.handleChange(e) } />
<button>Submit Comment</button>
</form>

防止违约非常重要

  handleSubmit = event => {
  event.preventDefault();    
  this.setState({ text: '' });
  };