title: Event handling stuck after removing element
body: <p>I have a button that changes icon when you drag the button down via Touch Event. Event handling logic is bound to Button containing the Icon component. When this component gets changed the whole event handling gets stuck.</p>
  <p>I thought that event would bubble through the icon and will work normally at the level of component actually handling the events, but sadly when icon is replaced this is stuck.</p>
  <p>Expected behavior: When you drag down the button down the icons should change all the way from icon 1 to 3, when you release icon changes back to icon 1</p>
  <pre><code>export const DragButton: React.FC = () =&gt; {
    const buttonRef = useRef&lt;HTMLDivElement&gt;(null);
    const [state, setState] = useState&lt;State&gt;(&quot;Idle&quot;);
    const [icon, setIcon] = useState(&lt;AccessAlarms /&gt;);
    const [mousePos, setMousePos] = useState&lt;{ x: number; y: number }&gt;({
      x: 0,
      y: 0
    });
  
    const onTouchStart = (e: React.TouchEvent) =&gt; {
      setState(&quot;Pressed&quot;);
    };
  
    const onTouchEnd = (event: React.TouchEvent) =&gt; {
      setState(&quot;Released&quot;);
      setIcon(&lt;AccessAlarms /&gt;);
    };
  
    const onTouchMove = (event: React.TouchEvent) =&gt; {
      setMousePos({ x: event.touches[0].clientX, y: event.touches[0].clientY });
      if (state == &quot;Pressed&quot;) {
        if (event.touches[0].clientY &gt; 200) {
          setIcon(&lt;Accessibility /&gt;);
        } else if (event.touches[0].clientY &gt; 100) {
          setIcon(&lt;ChangeHistory /&gt;);
        } else {
          setIcon(&lt;AccessAlarms /&gt;);
        }
      }
    };
  
    return (
      &lt;ButtonDiv
        ref={buttonRef}
        onTouchStart={(event) =&gt; onTouchStart(event)}
        onTouchEnd={(event) =&gt; onTouchEnd(event)}
        onTouchCancel={(event) =&gt; onTouchEnd(event)}
        onTouchMove={(event) =&gt; onTouchMove(event)}
      &gt;
        &lt;Button&gt;{icon}&lt;/Button&gt;
        {mousePos.y}
      &lt;/ButtonDiv&gt;
    );
  };
  </code></pre>
  <p>What can be causing this behavior? It seems that removal of element where event originated is the problem, how could this be solved?</p>
  <p>Sandbox is here, it is needed to use either Touchscreen or switch to mobile view in dew tools
  <a href="https://codesandbox.io/s/draggable-button-58vw2?file=/src/DragButton.tsx" rel="nofollow noreferrer">https://codesandbox.io/s/draggable-button-58vw2?file=/src/DragButton.tsx</a></p>