I am using React
and I have old-style kind of code for handling some events:
componentDidMount = () => {
document.addEventListener('mousedown', this.handleClick);
}
componentWillUnmount = () => {
document.removeEventListener('mousedown', this.handleClick);
}
Is it the right way of setting up event handlers in React
? I mean best practice. How would you refactor the above code in the React
- way of doing things? I understand when we set up events on individual elements, but I am confused in this particular case.