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.

Recycling issues when rerendering

See original GitHub issue

Hi, 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:closed
  • Created 7 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
developitcommented, Feb 17, 2017

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 👍

2reactions
cloudkitecommented, Jan 29, 2017

No pressure at all, easy enough to point to local build with recycling turned off. Thanks for your hard work on preact!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rendering is Recycling | Sustainable Benefits of Rendering
Animal fats produced from rendering, and the used cooking oil renderers collect from restaurants are cleaned are upcycled into these biofuels.
Read more >
Writing performant components | FlashList - Shopify
When an item gets out of the viewport, instead of being destroyed, the component is re-rendered with a different item prop. When optimizing...
Read more >
Reconciliation - React
Just to be clear, rerender in this context means calling render for all components, it doesn't mean React will unmount and remount them....
Read more >
Just Say No to Excessive Re-Rendering in React - GrapeCity
In this article, we will address instances of excessive re-rendering in React applications and demonstrate how to avoid them.
Read more >
5 Ways to Avoid React Component Re-Renderings
However, there are many approaches out there to avoid this issue… ... But, if we use the useMemo() Hook, we can avoid component...
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