-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Event handling is different between Preact and React #2887
Comments
I guess this is one of the core differences between using a custom event system and the one from the browser like we do. The browser's events bubble up like you described, whereas react changed that behavior. To prevent events from bubbling up there is the <button onClick={e => {
e.stopPropagation(); // Prevents event from bubbling up
setExpanded(false);
}}
/> |
The workaround could work if I would have a control over that code. Unfortunately, this is a 3rd party library and I can't change that. I might convince them to give preact 1st-class support, but this might take a while. For now it is great to have this ticket as an indicator of things that you can't do with Preact |
Which library are we talking about? Happy to PR a fix to them. |
The library is a closed-source enterprise. I could submit a PR myself, but this will not be enough. To make this work out, I need to also contribute them a full compatibility test suite, so they run all existing tests with Preact as well, to ensure there are no regressions. And this part is what makes it hard to sell. |
Your in luck as the same API is present in React too, although. So adding the Jason wrote an article a while back on the nuanced differences of delegation vs direct binding and why delegation isn't a good default: https://jasonformat.com/event-delegation-vs-direct-binding/ |
You could also try doing: import { options } from 'preact';
const oldEventOptions = options.event;
options.event = (e) => {
if (e.type === 'click' && e.target.id === 'myFlakeyButton') e.stopPropagation()
if (oldEventOptions) oldEventOptions(e);
} You can adjust that first check to just target the button you like |
This is fixed with switching to |
Fixed in #3608 |
I couldn't find something that clearly stated if Preact used event delegation by default. I see it does not use the synthetic event system, but I think it would be nice to be clear that there is also not event delegation by default. I had to dig through the issues to verify this was the case. preactjs/preact#2887 (comment)
Reproduction
There are two code sandboxes implementing the same thing in Preact and React
Steps to reproduce
Expected Behavior
The panel should close (and it does in React)
Actual Behavior
The panel does not close in Preact
This seem to be related to the way these frameworks handle events. React uses event delegation and Preact applies direct event listeners. Hence the
click
event bubbles up and reaches the panel root when it already was re-rendered, which breaks the toggle implementation.I found a similar issue #838 but since that one was already closed as resolved I opened a new one.
The text was updated successfully, but these errors were encountered: