1

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?

congce wang
  • 87
  • 2
  • 6

3 Answers3

0
    <input 
     ref={(input) => this.selectVal = input}
     onKeyPress={(e) => e.which === 13 ?this.onSearch():''}
    />
   onSearch = () => {
    console.log("value",this.selectVal.value);
   //  search(this.input.current.value); // some search api ...
   }

try this way

Kishan Jaiswal
  • 644
  • 3
  • 14
0

<input
  onChange={this.onInputChange}
  onKeyDown={this.onSearch}
/>

onInputChange = (e) => {
  this.setState({
    searchText: e.target.value
  }) 
}

onSearch = (e) => {
  if (e.keyCode === 13) {
    search(this.state.searchText); // some search api ...
  }
}
iambinodstha
  • 937
  • 10
  • 11
  • 1
    What's the difference between mine? – congce wang Mar 26 '19 at 21:07
  • Yeah there's a difference.The `KeyDown` event is triggered when the user presses a Key. while The `KeyPress` event is triggered when the user presses & releases a Key. – iambinodstha Mar 27 '19 at 02:32
  • Incorrect at least as far as I know/read. `keypress` is triggered immediately. The difference is actually: `keydown and keyup provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup, but as 97 by keypress. An uppercase "A" is reported as 65 by all events. `. [Reference](https://developer.mozilla.org/en-US/docs/Web/Events/keydown) – Elias Mar 27 '19 at 09:03
  • yeah i was wrong actually `KeyUp` event is triggered when the user presses & releases a Key. but `KeyDown` event is fired first than `KeyPress` event and `KeyPress` event will not fired in all key pressed like when BackSpace key is pressed `KeyPress` event will not fire. – iambinodstha Mar 27 '19 at 10:03
0

So I can't really understand why you use two separate functions.

First of all, if you only use searchText for the two functions you could just do:

HTML

<input
  onKeyPress={this.onKeyPress} />

JS

onKeyPress = e => {
  if(e.which === 13) {
    // Send Query
    search(e.target.value);
  }
}

And even if you needed searchText somewhere else you could just do:

onKeyPress = e => {
  let value = e.target.value;
  if(e.which === 13) {
    // Send Query
    search(value);
  } else this.setState({searchText: value});
}

If I missed something please tell me ^^

Elias
  • 3,592
  • 2
  • 19
  • 42