簡體   English   中英

在React JS上綁定touchstart的正確方法是什么?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM