D
D
Dmitry2017-05-24 17:23:34
JavaScript
Dmitry, 2017-05-24 17:23:34

How to remove an event using removeEventListener?

Hi everybody.
The question is: there is a class

class Test extends Component {

    onKeyDown(e) {
        console.log(e)
    }
    
    componentWillMount() {
        document.addEventListener('keydown', (e) => (this.onKeyDown));
    }
    render() {
       
        return (
            <div>test</div>
        )
    }
}

I'm trying to remove the event document.removeEventListener("keydown", this.onKeyDown); , but as I understand the event is not deleted because I have to give the arrow function that I give addEventListener and not onKeyDown. How to hang an event in this case and delete it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question