1

I'm making a comment form with React. I've almost built what I want except this little issue, where I'm not able to clear my input value, after I submit the form. What I typed in stays in the input field even after submitting.

My code:

CommentsIndex.js

class CommentsIndex extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      url: this.props.url,
      comment: '',
    };
    this.onSubmit2 = this.onSubmit2.bind(this);
  }
  loadCommentsFromServer() {
    $.ajax({
      // ajax call working fine
    });
  }
  onSubmit2(value) {
    const url = this.props.url;
    $.ajax({
      url: url,
      dataType: 'text',
      type: 'POST',
      cache: false,
      data: {
        comment: value
      },
      success: (data) => {
        this.loadCommentsFromServer();
      },
      error: (xhr, status, err) => {
        console.error(url, status, err.toString());
      },
    });
  }
  componentDidMount() {
    this.loadCommentsFromServer();
  }
  render() {
    return (
      <div>
        <NewComment onSubmit1={this.onSubmit2} />
        <Comments comments={this.state.comments} />
      </div>
    );
  }
}

export default CommentsIndex;

NewComment.js

class NewComment extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      comment: ''
    };
    this.getInput = this.getInput.bind(this);
    this.onSubmit = this.onSubmit.bind(this);
  }
  getInput(e) {
    this.setState({
      comment: e.target.value
    });
  }
  onSubmit() {
    this.props.onSubmit1(this.state.comment);
  }
  render() {
    return (
      <div>
        <input className="comment-field" onChange={this.getInput} />
        <input className="comment-submit" type="submit" value="SUBMIT" onClick={this.onSubmit} />
      </div>
    );
  }
}

export default NewComment;

I tried adding this.setState({ value: '' }) inside success: (data) => {} in my onSubmit2(), but it didn't work.

Any advise would be appreciated. Thanks in advance!

4
  • 1
    Possible duplicate stackoverflow.com/questions/38731271/… Commented Dec 7, 2018 at 4:14
  • Use ref={(input)=>this.input=input} in input text and in onClick set this.input.value=''. It will work for 100% Commented Dec 7, 2018 at 4:22
  • @RutvikBhatt Do you mean <input className="comment-field" onChange={this.getInput} ref={(input)=>this.input=input} />? And how can I set this.input.value='' in onClick when I already have onClick={this.onSubmit}? Sorry I'm a bit confused. Commented Dec 7, 2018 at 5:21
  • @ta539tg70 see my answer below Commented Dec 7, 2018 at 5:46

3 Answers 3

1

add a reference like ref={(input)=>this.commentInput=input} below:

<div>
    <input ref={(input)=>this.commentInput=input} className="comment-field" onChange={this.getInput} />
    <input className="comment-submit" type="submit" value="SUBMIT" onClick={this.onSubmit} />
</div>

and in your onSubmitMethod do the code like below:

onSubmit() {
    this.props.onSubmit1(this.state.comment);
    this.commentInput.value='';
}

set the value '' after callback to props

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

1 Comment

Worked perfectly. Thank you so much!
1

On success of ajax request, set state to '' as below:

this.setState({
  comment: ''
});

And bind state to input field as follows:

<input value={this.state.comment} className="comment-field" onChange={this.getInput} />

1 Comment

Aah, did not read the whole question. Assumed the ajax req and form are in same component. My bad.
0

If the input elements are inside a form, then

form.reset()

can be used to reset all the form components on submit.

class App extends React.Component {
  constructor(props) {
    super(props);
    this.onSubmit = this.onSubmit.bind(this);
  }
  onSubmit(event) {
    event.preventDefault();
    const form = event.target;
    const name = event.target.name.value;
    console.log(name);
    form.reset();
  }
  render() {
    return (
      <form onSubmit={this.onSubmit}>
        <input type="text" name="name" placeholder="Name" />
        <button>Submit</button>
      </form>
    );
  }
}
ReactDOM.render(<App />, document.getElementById("root"));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>

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.