1

So , I have a function that returns a form

return (
<div className="App">
  <header className="nav-container">
    <nav className="my-nav">
      <a href="/">
        <h1 className="brand-name">Admin Panel</h1>
      </a>
       <CgProfile id="pp" size={50}/>
    </nav>
  </header>

  <main className="main-body">
  <div id = "message"></div>
   <div className="form-container">
    <form>
    <select name = "genre" value = {this.state.value} onChange={this.handleChange}>
      <option value = ''>Select a Genre</option>
      <option value = "gk">General Knowledge</option>
      <option value = "gScience">General Science</option>
      <option value = "entertainment">Entertainment</option>
      <option value = "sports">Sports</option>
      <option value = "Politics">Maths</option>
      <option value = "geography">Geography</option>
    </select>

      <input type="text" placeholder="Enter Question" spellCheck="true" name="question" value={this.state.value} onChange={this.handleChange}></input>
      <input type="text" placeholder="Option 1" spellCheck="true" name="option1" value={this.state.value}  onChange={this.handleChange}></input>
      <input type="text" placeholder="Option 2" spellCheck="true" name="option2" value={this.state.value}  onChange={this.handleChange}></input>
      <input type="text" placeholder="Option 3" spellCheck="true" name="option3" value={this.state.value}  onChange={this.handleChange}></input>
      <input type="text" placeholder="Option 4" spellCheck="true" name="option4" value={this.state.value}  onChange={this.handleChange}></input>
      
      <button type="button" onClick={this.handleSubmit}>Add</button>
    </form>
    </div>
  </main>
</div>
);

When the Add button is clicked, the form gets submitted but when I use the setState method to clear the form input field the form field is not being cleared.

My handleSubmit function is:

    handleSubmit = (e)=>{
      e.preventDefault();
      if(this.state.question === '' || this.state.option1 === '' || this.state.option2 === '' || this.state.option3 === '' || this.state.option4 === '' || this.state.genre === '' ){
      var errorMessage = React.createElement('p',{className : "err"},"All fields are required");
     }
    else{
    let queObj = {
      question : this.state.question,
      option : {
        1 : this.state.option1,
        2 : this.state.option2,
        3 : this.state.option3,
        4 : this.state.option4
      },
      genre : this.state.genre,
      submittedBy : "Abhilekh Gautam",
      Date : new Date().toDateString()

    }
   que.push(queObj);
   this.setState({
     question:'',
     option1:'',
     option2:'',
     option3:'',
     option4:'',
     genre:''
   },()=>{
     console.log('SetState called')
     console.log(this.state.question.value)
 })

 var sucessMessage = React.createElement('p',{className : "sucess"},"Question Submitted Sucessfully");


  }

  if(errorMessage)
     ReactDOM.render(errorMessage,document.getElementById('message'))
  
  else if(sucessMessage)
     ReactDOM.render(sucessMessage,document.getElementById('message'));
}

EDIT:1 My handleChange function is

handleChange = (e) =>{
  e.preventDefault();
   this.setState(
     {
       [e.target.name]: e.target.value
    }
   )
}
8
  • setState is asynchrone, which means that when you call console.log(this.state.question.value), the value of the state has probably not changed yet Commented Sep 6, 2021 at 12:37
  • @TheoMartinez Yeah I was just trying to debug Commented Sep 6, 2021 at 12:38
  • You should use the excellent React devtools add on on your browser to explore current state in your application Commented Sep 6, 2021 at 12:39
  • Why you know state doesn't change Commented Sep 6, 2021 at 12:39
  • @Viet if the state was changed, it would have probably emptied my input field Commented Sep 6, 2021 at 12:40

1 Answer 1

1

Because you don't set value of input to value you reset on submit so the value of input will notchange. Just update value of input to exact state:

<input type="text" placeholder="Option 1" spellCheck="true" name="option1" value={this.state.option1}  onChange={this.handleChange}></input>
Sign up to request clarification or add additional context in comments.

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.