ta539tg70
ta539tg70

Reputation: 317

Clearing an input field after submit with React

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!

Upvotes: 1

Views: 1636

Answers (3)

Agney
Agney

Reputation: 19224

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>

Upvotes: 0

Rutvik Bhatt
Rutvik Bhatt

Reputation: 3296

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

Upvotes: 1

Vishal
Vishal

Reputation: 10964

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} />

Upvotes: 1

Related Questions