Using only onChange
and value and while focused inside a <input/>
, preferably without jQuery, is there a way to trigger a method by pressing the 'Enter' key? Because, would only want the user to press 'Enter' key in order to update the name
string state.
Here is the code:
constructor(props) {
super(props);
this.state = {
name: '',
}
}
textChange(event) {
this.setState({
name: event.target.value,
})
}
//Would like to trigger this once 'Enter' key is pressed while focused inside `<input/>`
enterPressed() {
var name = this.state.name;
}
render() {
return (
<input
placeholder='Enter name'
onChange={this.textChange.bind(this)}
value={this.state.name}
/>
)
}