onKeyDown event not working on divs in React

前端 未结 7 1133
北恋
北恋 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:36

    You should use tabIndex attribute to be able to listen onKeyDown event on a div in React. Setting tabIndex="0" should fire your handler.

提交回复
热议问题