onKeyDown event not working on divs in React

前端 未结 7 1153
北恋
北恋 2020-12-02 11:11

I want to use a keyDown event on a div in React. I do:

  componentWillMount() {
      document.addEventListener(\"keydown\", this.onKeyPressed.bind(this));
          


        
7条回答
  •  时光说笑
    2020-12-02 11:26

    You need to write it this way

    If onKeyPressed is not bound to this, then try to rewrite it using arrow function or bind it in the component constructor.

提交回复
热议问题