onKeyDown event not working on divs in React

前端 未结 7 997
北恋
北恋 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:42

    The answer with

    <div 
        className="player"
        onKeyDown={this.onKeyPressed}
        tabIndex={0}
    >
    

    works for me, please note that the tabIndex requires a number, not a string, so tabIndex="0" doesn't work.

    0 讨论(0)
提交回复
热议问题