2017-08-11 226 views
0

我正在尝试添加被动事件监听器。这是我的JSX代码的一部分:被动事件监听器

<div id="test" onTouchStart={{ handler: TouchFunc(), passive: true }}></div> 

而且功能:

function TouchFunc(e){ 
console.log(e); 
} 

输出是:

undefined

同时,我得到这个错误:

Expected onTouchStart listener to be a function, instead got type object

如何从该功能访问该事件上?

回答

1

onTouchStart应该是一个函数。将此行更改为:

<div id="test" onTouchStart={this.TouchFunc}></div> 

要访问它的功能:

function TouchFunc(e){ 
    console.log(e.target.id); 
} 
1

现在你调用函数TouchFunc组件中呈现。您应该使用此代码:

<div id="test" onTouchStart={{ handler: TouchFunc, passive: true }}></div>