question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Event handling is different between Preact and React

See original GitHub issue

Reproduction

There are two code sandboxes implementing the same thing in Preact and React

Steps to reproduce

  1. Click on “Expand me”
  2. Click on “close”

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 https://github.com/preactjs/preact/issues/838 but since that one was already closed as resolved I opened a new one.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
JoviDeCroockcommented, Jul 6, 2022

This is fixed with switching to setTimeout for debouncing as well, we should include this or an alternative scheduler for 11 https://codesandbox.io/s/cocky-star-bw9sgv?file=/src/index.js

1reaction
JoviDeCroockcommented, Jan 5, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Differences to React - Preact
Preact uses the browser's native addEventListener for event handling. See GlobalEventHandlers for a full list of DOM event handlers. A full events ...
Read more >
Preact vs. React: A Quick Comparison - HackerNoon
Event Handling: React implements its own synthetic event system for event handling, reducing app performance, while Preact has no synthetic ...
Read more >
Preact vs. React | Syncfusion Blogs
The primary difference between React and Preact is that React uses synthetic events, which are wrapped around the actual events that React ...
Read more >
React vs Preact vs Inferno - DEV Community ‍ ‍
Preact · There is no synthetic event system. React ships its own (very heavy) Synthetic Event system that offers a host of benefits...
Read more >
React Js Vs Preact: Application Development Made Easier
Preact, on the other hand, renders more quickly. ... React uses its own synthetic event system for event handling, which reduces app performance, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found