I know some react but I am stuck in a weird situation.
I have two inputs and a button, the button should be enabled when both inputs are not empty. So, I used a state property for each input value and also one property telling me if both inputs have value:
this.state = {
title: '',
time :'',
enabled : false
}
also I have a onChange for each input to set State accordingly:
<input type="text" id="time" name="time" onChange={this.onChange.bind(this)} value={this.state.time}></input>
<input type="text" id="title" name="title" onChange={this.onChange.bind(this)} value={this.state.title}></input>
and the onChange is like this
onChange(e){
this.setState({
[e.target.id] : e.target.value,
enabled : ( (this.state.title==='' || this.state.time==='' ) ? false : true)
});
}
the problem is that the setState looks at previous state and the enabled is always one step behind, so if I type X in first and Y on second, still the enabled would be false.
I managed to solve it by using a setTimeout and taking the second line in it but it looks wrong to me.
onChange(e){
this.setState({
[e.target.id] : e.target.value,
});
setTimeout(() => {
this.setState({
enabled : ( (this.state.title==='' || this.state.time==='' ) ? false : true)
});
}, 0);
}
Any better solutions?