How to get input textfield values when enter key is pressed in react js? How to get input textfield values when enter key is pressed in react js? javascript javascript

How to get input textfield values when enter key is pressed in react js?


Use onKeyDown event, and inside that check the key code of the key pressed by user. Key code of Enter key is 13, check the code and put the logic there.

Check this example:

class CartridgeShell extends React.Component {   constructor(props) {      super(props);      this.state = {value:''}      this.handleChange = this.handleChange.bind(this);      this.keyPress = this.keyPress.bind(this);   }     handleChange(e) {      this.setState({ value: e.target.value });   }   keyPress(e){      if(e.keyCode == 13){         console.log('value', e.target.value);         // put the login here      }   }   render(){      return(         <input value={this.state.value} onKeyDown={this.keyPress} onChange={this.handleChange} fullWidth={true} />      )    }}ReactDOM.render(<CartridgeShell/>, document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script><div id = 'app' />