How do I detect Esc keypress on reactjs? The similar thing to jquery
$(document).keyup(function(e) {
if (e.keyCode == 27) { // escape key maps to keycode `
You'll want to listen for escape's keyCode
(27) from the React SyntheticKeyBoardEvent onKeyDown
:
const EscapeListen = React.createClass({
handleKeyDown: function(e) {
if (e.keyCode === 27) {
console.log('You pressed the escape key!')
}
},
render: function() {
return (
)
}
})
Brad Colthurst's CodePen posted in the question's comments is helpful for finding key codes for other keys.