Recycling issues when rerendering
See original GitHub issueHi, maybe it’s not a bug at all, but I have some strange behavior in my project. I wasn’t able t reproduce it in jsfiddle so here’s what I’ve discovered so far:
I have a button with attached onClick
handler. When component containing this button
is unmounted button
HTMLElement got ‘recycled’ by
https://github.com/developit/preact/blob/master/src/dom/recycler.js#L15
(nodes[name] || (nodes[name] = [])).push(node);
then when another instance of same component is mounted this button
HTMLElement is getting back in
https://github.com/developit/preact/blob/master/src/dom/recycler.js#L22
node = nodes[name] && nodes[name].pop()
It has event handler onClick
attached from previous use.
Actually it’s not an onClick
, but it’s node._listeners
object with this handler.
And new event handler is not attached cause node ALREADY HAVE _listeners
hash with given key from first time use pointing to an old function.
https://github.com/developit/preact/blob/master/src/dom/index.js#L60
if (!l[name]) node.addEventListener(name, eventProxy, !!NON_BUBBLING_EVENTS[name])
I’ve fixed it locally by removing any node recycling and creating new nodes every time. Can you give me a short explanation why recycler work like this (storing element by element name and not taking into account other props)?
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (6 by maintainers)
Top GitHub Comments
Yes! Recycling an element is probably not removing extra children, now that Preact allows those. Good point, and another nail in this coffin. Can’t remove it soon enough 👍
No pressure at all, easy enough to point to local build with recycling turned off. Thanks for your hard work on preact!