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
Erratic behaviour for simple example with toggling a link #3596
Comments
I agree with you that this feels buggy, generally however this is a quirk in the diffing. We are introducing different levels in diffing and it seems to not carry over the DOM correctly. Will look into whether we can fix this. There are a few solutions around this luckily:
|
Interesting, I actually tried with adding id to the items, but I should ofc have tried with key since that is what is internally used. I added keys for the divs and it started to work. Seems a bit scary, so maybe at least add a warning if items don't have keys? Huge thanks for your swift response! |
It looks like we're rendering too quickly, before the event is fully dispatched. When the event bubbles up to the outer |
Peculiar thing is that if I call |
FWIW the reason this happens is because we batch state updates using a microtask ( This was the main reason I'd considered advocating for us switching |
Fixed in #3608 |
Describe the bug
A very simple link that toggles "itself" with useState behaves erratically. The change happens twice, depending on structure of underlying html. See below (or also https://stackoverflow.com/questions/72803084/simple-example-with-preact-is-firing-twice-on-usestate). I am using v10.4.6.
It's a bit far fetched that this has not been discovered before, so I suspect there is something going on here that is not a bug in Preact itself. But I cannot think if what it might be.
To Reproduce
Notice the difference in the html for various cases below. They should all work the same (unless there is something about Preact I don't understand yet).
The following does not work. The text does not toggle, but console log shows it toggle back and forth for each click.
https://codesandbox.io/s/compassionatwwwe-bogdan-2h5rko-2h5rko?file=/index.js
The following also does not work, but behaves differently. The first click works as expected and subsequent once do not.
https://codesandbox.io/s/compassionatwww1e-bogdan-2h5rko-forked-dzheqi?file=/index.js
The following works as expected:
https://codesandbox.io/s/compassionatwww1e-bogdan-2h5rko-forked-njtlv6?file=/index.js
Expected behavior
All three examples should properly toggle the text for each click.
The text was updated successfully, but these errors were encountered: