I have a requirement that after typing certain content in an tag, pressing enter will do search function.
It running well normally like:
<input
onChange={this.onInputChange}
onKeyPress={this.onSearch}
/>
onInputChange = (e) => {
console.log(2);
this.setState({
searchText: e.target.value
})
}
onSearch = (e) => {
console.log(1);
if (e.which === 13) {
search(this.state.searchText); // some search api ...
}
}
But if user Enter really quickly, like 0.1s, the this.state.searchText
is not get updated properly.
This is not just caused by setState
is async method, but the onKeyPress
is trigger before onChange
.
is there any idea to deal with this issue?