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.

Diff behaviour not as expected - preact unmounting then remounting the same DOM nodes even when there is a key

See original GitHub issue

I have found that preact is unecessarily tearing down and rebuilding DOM nodes even when a key is used (example codepen)

In this case even though the key is supplied almost all of the DOM nodes are being removed and then added again even though only three are changed

EDIT TO be clear it is expected that the components are being rerendered, but the issue is that all of the DOM nodes (not virtual dom) are being removed and then re-added again

(before changing the document):

screen shot 2018-03-31 at 9 31 36 pm

(after changing the document):

screen shot 2018-03-31 at 9 31 38 pm

After debugging I eventually found that each time the new document was selected the span elements (in the real DOM) inside the content were being removed and then added again, and the weird thing is that the spans that are being removed are exactly the same as the spans that are being added (as in they were the same instance)

Expected Behaviour Only a few DOM nodes will be changed (as keys are supplied and most of the vdom remains the same)

Actual Behaviour All of the DOM nodes are being removed and then re-added again

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
lewischacommented, Sep 28, 2018

That’s great news! Is there a timeline for that release?

3reactions
yaodingydcommented, Sep 28, 2018

@lewischa this is difficult to fix in the currently version of Preact, but it has been fixed in the next major version.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Avoid unnecessary remounting of DOM elements in React
I expected React to not unmount and then remount the two DOM nodes corresponding to the two buttons when switching from View1 to...
Read more >
React prevent remounting components passed from props
Expected behavior is for every page to only trigger the mounting once. I know I can probably fix this by using Redux or...
Read more >
Strict Mode - React
It creates a refactoring hazard where you can't change the implementation details of a component because a parent might be reaching into its...
Read more >
Understanding React's key prop - Kent C. Dodds
If you return the same element types, it keeps those components/DOM nodes around, even if all the props changed. For more on this,...
Read more >
React key attribute: best practices for performant lists
Looking into how React "key" attribute works, how to use it correctly, how to improve performance of lists with it, and why array...
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