Skip to content

onPress Issue - Click Through Component When it Unmounts #7993

@mmarcuccio

Description

@mmarcuccio

Provide a general summary of the issue here

In a certain case, onPress is sometimes firing onClick event handlers for elements beneath the target. This occurs when the onPress handler updates state which results in the component being unmounted.

🤔 Expected Behavior?

When using a touch device to press a react-aria-components Button (which usesusePress internally), it is expected that the Button will prevent propagation of that event.

This is usually the case as you can see below. In this case, we have two buttons. The top button is a react-aria-components Button using onPress. The element beneath is a standard HTML button with an onClick handler that fires an alert if clicked.

Image

😯 Current Behavior

However, if the onPress handler results in the component being unmounted, the event sometimes propagates to the element beneath (seems to be inconsistent - maybe a race condition?).

Below is the same situation as before, but we conditionally render the react-aria-components Button based on React state, and in the onPress handler we toggle that state.

Image

https://codesandbox.io/p/sandbox/suspicious-wilbur-k6v4l6

💁 Possible Solution

No response

🔦 Context

No response

🖥️ Steps to Reproduce

Code sandbox repro link: https://codesandbox.io/p/sandbox/suspicious-wilbur-k6v4l6

  1. Open Chrome dev tools and simulate a mobile device via device mode
  2. Tap the top button.
  3. onPress Event handler fires for top button, and top button is removed due to state update.
  4. onClick Event handler fires for button underneath.
    This might take a few tries as it is inconsistent.

Version

Latest react-aria-components 1.7.1

What browsers are you seeing the problem on?

Chrome

If other, please specify.

No response

What operating system are you using?

Mac OS 15.3.1

🧢 Your Company/Team

No response

🕷 Tracking Issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions