Improving Touch Events upon an Infinite Scrolling Component
Tags: javascript, touchevents, react
Published: 2019-06-10 18:07:48

---
title: Improving Touch Events upon an Infinite Scrolling Component
published: true
tags: javascript, touch-events, react
canonical_url: https://www.wesleylhandy.net/blog/improving-touch-events-upon-infinite-scrolling-component.html
---

[![Closeup of a butterfly sitting on the tip of the someone's index finger](https://www.wesleylhandy.net/static/9262257e3e1dba7123eb7236d14248d8/c35de/butterfly-finger.jpg)](/static/9262257e3e1dba7123eb7236d14248d8/de376/butterfly-finger.jpg)

In my recent blog post on [Using React Hooks to set up Infinite Scroll](https://www.wesleylhandy.net/blog/infinite-scroll-mobile-desktop-gatsby.html), I created a working version of infinite scroll that works in both desktop and touch screen environments. However, I came across a problem I did not anticipate when I put it into production, **some of the links on my page stopped working**. I tried debugging my CSS, to no avail, only to realize the solution involved updating my `touchend` event handlers with a very simple fix.

## <svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>Solution: Debugging CSS ??

The structure of my components nests some links within block level elements. To acheive infinite-scrolling, I map over a list of businesses as follows:

```
<li key={govId} index={idx + 1}>
  <h2>
    <Link 
      to={ `/businesses/${businessName}` } state={{ 
        prevPath: typeof window !== `undefined` ? window.location.pathname : ''
      }}
    >
      {businessName}
    </Link>
  </h2>
  <p>
    <a href={`tel:${businessPhone}`}>{businessPhone}</a>
  </p>
</li>
```

From past experience, I immediately thought the solution would be to either adjust the `z-index` of the nested link or to set `pointer-events` on the parent elements. I tried both:

```
    li, li>h2, p { 
        pointer-events: auto;
        z-index: 1;
    }
    li>h2>a, p>a {
        z-index: 3;
    }
```

Neither solution solved the problem nor were a problem to begin with, at least in my implementation.

## <svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>Solution: Debugging `touchend` Event Handler !!

First, here is the initial state of my code for this component. I define `handleScroll` to add more items to the infinite scroll. Then, `handleTouchEnd` calls the scroll event handler to avoid double loading. Take particular note of `e.preventDefault` (perhaps this shouldn’t be called at all? We shall find out soon enough):

```
  const handleScroll = () => {
    if ( !hasMore ) return;
    if ( window && 
       ( window.innerHeight + 
         document.documentElement.scrollTop >=
         document.documentElement.offsetHeight ) 
    ) {
      loadMore() // function to add more items to the infinite scroll until no items left
    }
  }

  const handleTouchEnd = (e) => {
      e.preventDefault(); handleScroll();
  }
  
  useEffect(() => {
    window && window.addEventListener('touchend', handleTouchEnd)
    window && window.addEventListener('scroll', handleScroll)
    window && window.addEventListener('resize', handleScroll)
    return () => {
      window && window.removeEventListener('touchend', handleTouchEnd)
      window && window.removeEventListener('scroll', handleScroll)
      window && window.removeEventListener('resize', handleScroll)
    };
  }, [businesses, hasMore])
```

After realizing that updated my `css` would not resolve my issue, I wondered what in the world is causing my problem. In this particular component, I use two `React` hooks to manage state and handle events - `useState` and `useEffect`. I wondered,

> Okay, so this is a `Gatsby` project, and I'm importing the `Link` component from the gatsby library. Could these hooks be interfering with the functionality of the `Link`?

This wasn’t the problem. I could see in the console that `Link` rendered a simple `a` tag, and other `Link` components were working on the page, such as those rendered by the `Navigation` component. The only links not working were within my infinite scrolling list component. Moreover, the links worked perfectly in a desktop environment. So I again wondered,

> Why in the world does this work on desktop and not on mobile? No errors are being thrown. The `href` attribute is valid and working if I paste it in the browser. How again does the `touchend` event work?

This led me to investigating the order in which events are fired by touch screens.

### <svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>`click` comes after `touchend`

According to MDN, the W3C standard calls for [a _typical_ order of events fired by touch screens](https://developer.mozilla.org/en-US/docs/Web/API/Touch_events/Supporting_both_TouchEvent_and_MouseEvent#Event_order), as follows:

- `touchstart`
- Zero or more `touchmove` events, depending on movement of the finger(s)
- `touchend`
- `mousemove`
- `mousedown`
- `mouseup`
- `click`

Remember I asked you to take particular note that I was calling `e.preventDefault()` on the `touchend` event? Turns out that is the culprit. By cancelling the dispatching of further events on `touchend`, the `click` event for the link component was never being fired. As MDN tells us:

> If the `touchstart`, `touchmove` or `touchend` event is canceled during an interaction, _no mouse or click events will be fired_.

The solution then must include not calling `e.preventDefault`, particularly when a link is the target of `touchend`.

So, the only change necessary involves adding a condition within my `handleTouchEnd` function, to check for `a` tags, or `Element.tagName == "A"`, and only call `e.preventDefault()` if the target is not such a tag:

```
const handleTouchEnd = (e) => {
  if (e.target.tagName !== "A") {
    e.preventDefault(); 
    handleScroll();
  } else {
    console.log("this makes me a click event, most likely")
  }
}
```

> Photo by [Amy Humphries](https://unsplash.com/@amyjoyhumphries) on [Unsplash](https://unsplash.com/)