参考:https://stackoverflow.com/questions/27827234/how-to-handle-the-onkeypress-event-in-reactjs
以前过时的做法:使用keyCode
var Test = React.createClass({
add: function(event){
if(event.keyCode == 13){
alert('Adding....');
}
},
render: function(){
return(
<div>
<input type="text" id="one" onKeyPress={this.add} />
</div>
);
}
});
React.render(<Test />, document.body);
现在建议的做法:使用语义化的key
handleKeyPress = (event) => {
if(event.key === 'Enter'){
console.log('enter press here! ')
}
}
render: function(){
return(
<div>
<input type="text" id="one" onKeyPress={this.handleKeyPress} />
</div>
);
}
来源:oschina
链接:https://my.oschina.net/u/4691589/blog/4954729