[英]What's the proper way of binding touchstart on React JS?
我正在学习React JS,截至目前我在源代码,测试或官方文档上找不到任何示例。 在文档上,据说支持touchStart
事件,但handleTouchStart
不起作用。
我发现的唯一地方和实际工作的例子是反应式触摸项目。
以下代码有效,但这是唯一的绑定方式吗? 对我来说,它看起来像是一种廉价的解决方法
var MyHeader = React.createClass({
handleTouchStart: function() {
console.log('handleTouchStart');
},
render: function() {
return this.transferPropsTo(
<header onTouchStart={this.handleTouchStart}>{title}</header>
);
}
};
这样做的实际方法是什么?
从React v0.14开始,你不必调用React.initializeTouchEvents(true);
手动了。
http://facebook.github.io/react/blog/2015/10/07/react-v0.14.html#breaking-changes
在使用触摸事件时,我将React.initializeTouchEvents(true)
添加到componentWillMount
组件生命周期方法,它似乎正常工作。
var MyHeader = React.createClass({
componentWillMount: function(){
React.initializeTouchEvents(true);
},
handleTouchStart: function() {
console.log('handleTouchStart');
},
render: function() {
return this.transferPropsTo(
<header onTouchStart={this.handleTouchStart}>{title}</header>
);
}
};
Hy,Iraê!
您必须在任何渲染之前调用React.initializeTouchEvents(true)。 检查反应文档: http : //facebook.github.io/react/docs/events.html#touch-events
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.