Spoiler alert: this post contains answers to React: Add Event Listeners
https://learn.freecodecamp.org/front-end-libraries/react/add-event-listeners/
According to the page explanation,
componentWillUnmount(){
document.removeEventListener("keydown", this.handleKeyPress)
}
will remove the event listener previously attached in
componentDidMount(){
document.addEventListener("keydown", this.handleKeyPress)
}
I thought that this would mean the event would only fire only once, but if I continuously press on the ‘Enter’ key, the message prints out each time: You pressed the enter keyYou pressed the enter keyYou pressed the enter key, etc, etc.
Could any one explain, in the simplest possible English :),
- why we would want to remove the event listener and
- what actually happens in the browser (if anything) when we do?