Skip to content

Instantly share code, notes, and snippets.

@pechitook
Created October 3, 2017 15:15
Show Gist options
  • Save pechitook/4d0bc15f071eb087159d43942328497e to your computer and use it in GitHub Desktop.
Save pechitook/4d0bc15f071eb087159d43942328497e to your computer and use it in GitHub Desktop.
import { Component } from 'react';
import PropTypes from 'prop-types';
class KeyEvent extends Component {
listen = (event) => {
if (event.key === this.props.when) {
this.props.do();
}
}
componentDidMount() {
document.addEventListener(this.props.trigger, this.listen);
}
componentWillUnmount() {
document.removeEventListener(this.props.trigger, this.listen);
}
shouldComponentUpdate() {
return false;
}
render() {
return null;
}
}
KeyEvent.propTypes = {
trigger: PropTypes.string.isRequired,
when: PropTypes.string.isRequired,
do: PropTypes.func.isRequired,
};
export default KeyEvent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment