反应:addEventListener 不是函数错误

scu*_*lly 6 reactjs

我在组件的渲染方法中有这个 div

<div ref={node => this.domNode = node} style={this.getStyle()}>{ this.props.children }</div>
Run Code Online (Sandbox Code Playgroud)

当我在 componentDidMount 中执行此操作时

this.domNode.addEventListener('mousedown', this.onDrag); 
Run Code Online (Sandbox Code Playgroud)

有错误

this.domNode.addEventListener is not a function
Run Code Online (Sandbox Code Playgroud)

Sca*_*ize 1

noderef 回调的argumentmnt( ) 可以是null。在绑定侦听器之前,您需要检查这一点。

请注意,当引用的组件被卸载时以及每当 ref 发生更改时,旧的 ref 都会以 null 作为参数被调用。

来自https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute