简体   繁体   English

将被动addEventListener替换为onTouchStart方法

[英]Replace passive addEventListener to onTouchStart method

I want to replace document.addEventListener('touchstart', handler, {passive: true}); 我想替换document.addEventListener('touchstart', handler, {passive: true}); to onTouchStart={handler} onTouchStart={handler}

So if I had passive flag on true then my handle method should like: 因此,如果我在true上设置了被动标志则我的handle方法应为:

const handler = (event) => { 
    console.log(event); 
} 

and when I have a flag on false : 当我在false上有一个标志

 const handler = (event) => { 
    event.preventDefault();
    console.log(event); 
} 

I'm talking about Passive Event Listeners and browser behavior. 我说的是被动事件监听器和浏览器行为。 Is it only about using preventDefault() or not ? 仅仅是关于使用preventDefault()吗? Do I understand it correctly or not ? 我是否正确理解?

Looking at the docs here 这里看文档

Currently, browsers can't know if a touch event listener is going to cancel the scroll, so they always wait for the listener to finish before scrolling the page. 当前,浏览器无法知道触摸事件侦听器是否要取消滚动,因此它们始终等待侦听器完成才滚动页面。 Passive event listeners solve this problem by enabling you to set a flag in the options parameter of addEventListener indicating that the listener will never cancel the scroll. 被动事件侦听器通过允许您在addEventListener的options参数中设置一个标志来解决此问题,该标志指示侦听器将永远不会取消滚动。

This means that you change the behavior of the event. 这意味着您可以更改事件的行为。 If you only change the preventDefault , it still have the normal behavior of the event (not passive). 如果仅更改preventDefault ,则它仍具有事件的正常行为(不是被动的)。 So this doesn't change anything. 所以这不会改变任何东西。

The reason why in the docs they say to "remove" the preventDefault is that you need the default action of the event when setting passive to true. 他们在文档中说“删除” preventDefault是,当将被动设置为true时,您需要事件的默认操作。

This will be the normal event, without passive. 这将是正常事件,而不是被动事件。

onTouchStart={handler}

This will add the passive action, but remove because of preventDefault 这将添加被动操作,但由于preventDefault而将其删除

const handler = (event) => { 
    event.preventDefault();
    console.log(event); 
} 
...
onTouchStart={handler}

So you need the .addEventListener with {passive: true} and also remove any call of .preventDefault() . 所以,你需要.addEventListener{passive: true} ,也去除任何呼叫.preventDefault()

Here is an example of how to do this in any component. 这是一个如何在任何组件中执行此操作的示例。

class SomeClass extends React.Component {

  componentDidMount() {
    this.elementYouWant.addEventListener("touchstart", this.handleTouchStart, {passive: true});
  }

  componentWillUnmount() {
    this.elementYouWant.removeEventListener("touchstart", this.handleTouchStart);
  }

  handleTouchStart = (e) => {
    console.log(e);
  }

  render() {
    return (
      <div ref={e => this.elementYouWant = e}>
        ...
      </div>
    );
  }

}

I searched on how to pass the option paramenter to the event listener without needing to create it in componentDidMount and componentWillUnmount in react, but coudn't find it. 我搜索了如何将选项参数传递给事件侦听器,而无需在react中的componentDidMountcomponentWillUnmount中创建它,但是找不到它。

You can see here the best way of implementing it because some browser will interpret {passive: true} as true and this will create a different behavior. 您可以在此处看到实现它的最佳方法,因为某些浏览器会将{passive: true}解释为true ,这将创建不同的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM