Current Article:

How to handle the onKeyPress event in React?

How to handle the onKeyPress event in React?

Use onKeyPress and event.key to handle the on key press event:

handleKeyPress = (event) => {
  if (event.key === 'Enter') {
    console.log('enter press here! ')
  }
}
render: function() {
  return(
    <div>
      <input type="text" id="one" onKeyPress={this.handleKeyPress} />
    </div>
  );
}