I reached the challenge of adding/removing events in componentDidMount
and componentWillUnmount
, however I got confused with your solution:
componentDidMount() {
// Attach an event
document.addEventListener("keydown", this.handleKeyPress)
}
componentWillUnmount() {
document.removeEventListener("keydown", this.handleKeyPress)
While my solution is :
componentDidMount() {
// Attach an event
document.addEventListener("keydown", event => this.handleKeyPress(event))
}
componentWillUnmount() {
document.removeEventListener("keydown", event => this.handleKeyPress(event))
}
But it does not pass the test. Why?