I want to replace document.addEventListener('touchstart', handler, {passive: true}); to onTouchStart={handler}
So if I had passive flag on true then my handle method should like:
const handler = (event) => {
console.log(event);
}
and when I have a flag on 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 ?
Do I understand it correctly or not ?
Looking at the docs here
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). So this doesn't change anything.The reason why in the docs they say to "remove" the
preventDefaultis that you need the default action of the event when setting passive to true.This will be the normal event, without passive.
This will add the passive action, but remove because of
preventDefaultSo you need the
.addEventListenerwith{passive: true}and also remove any call of.preventDefault().Here is an example of how to do this in any component.
I searched on how to pass the option paramenter to the event listener without needing to create it in
componentDidMountandcomponentWillUnmountin react, but coudn't find it.You can see here the best way of implementing it because some browser will interpret
{passive: true}astrueand this will create a different behavior.