I am submitting a form that takes input and it has handleInput function that sets the state. If i enter first letter and try to log it it shows blank in console and when i press again it is shown. I was wondering if I were to use this event in search something then a letter will always be less or maybe I am doing something wrong.
My Input field and handleInput function
<input type="text" className="form-control" value={this.state.title} name="title" onChange={this.handleInput} />
My handleInput function and state
state = {
title:'',
company:'',
}
handleInput = (e) =>
{
this.setState({[e.target.name]:e.target.value})
console.log(this.state.title)
}
========================== SOLUTION ================================
I found the solution on another question this.state provides option of call back function so you can do like this
this.setState([e.target.name]:e.target.value,()=>{
this.updatedValues(this.state)
})
updatedValues=(val)=>{
console.log(val.title)
}
Now the values will be updated

Aucun commentaire:
Enregistrer un commentaire