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.

Unnecessary unmounting/mounting children with keys

See original GitHub issue

Hi all, I’m the author of tikzcd-editor and am experiencing severe performance degradation after updating Preact recently.

tikzcd-editor is rendering a grid of elements onto the viewport; we only render visible grid cells. The user can use the pan tool to pan the grid, so cells outside of the viewport need to be added while panning, and cells that go outside of the viewport get removed.

Each grid cell will be typeset using MathJax on every change, which is expensive, that’s why every grid cell has a key set to its coordinates, so we avoid rerendering while panning. This worked fine in v10.4.1, but in v10.4.2 and the latest version v10.5.3, we experience a lot of unnecessary rerenders.

Reproduction

Steps to reproduce

Here’s a deployment of tikzcd-editor with v10.5.3 with a sample diagram. When trying to pan the viewport, you can see it’s not smooth at all. When opening the Devtools you can see that Preact is unnecessarily unmounting/mounting elements.

Expected Behavior

Visible children should not be unmounted/remounted like in v10.4.1:

Good behavior

Actual Behavior

A lot of unnecessary unmounting/rendering of children as experienced in v10.4.2 and v10.5.3:

Bad behavior

Issue Analytics

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

github_iconTop GitHub Comments

6reactions
darsaincommented, Nov 6, 2020

Was told my issues is related, so here is my case report:

Whenever you remove an uncertain number of elements from a keyed list, the elements after them get remounted. The number seems to differ between environments. On my local app it’s 20+, on the isolated example below it’s 50+.

Here is a visualization of the issue. Elements that get remounted flash red. The last 20 items should never flash, as only range 20-80 is being toggled:

xG61GfMSA5

This doesn’t happen for smaller amount of items being removed:

VVaLqUrt3k

The issue is isolated here: https://codesandbox.io/s/cocky-joliot-9gw5v

3reactions
JoviDeCroockcommented, Dec 13, 2021

@jaydenseric this has been fixed in v11, introducing this fix in v10 would be a huge perf impediment I’ve tried in https://github.com/preactjs/preact/pull/3359 for v11 with our new diffing algo it is a net positive https://github.com/preactjs/preact/pull/3358

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to avoid unmounting of children components when using ...
To log on unmount, use useEffect(fn, []) . As you used the key property correctly, the components are not remounted, just rerendered.
Read more >
Avoid unnecessary remounting of DOM elements in React
I ran into a strange problem while trying to use React's built-in animation API to fade in/out DOM elements when they enter/exit the...
Read more >
React - Fun with keys - Cmichel.io
React will compare the keys of the new element with the previous keys and 1) mount components having a new key 2) unmount...
Read more >
React mounting/unmounting of children (no remounting)
Demo how to prevent unnecessary re-mounting of DOM nodes by React....
Read more >
Using React's Key Attribute to remount a Component - Nik Graf
When children have keys, React uses the key to match children in the ... a useEffect logging out when the component mounts and...
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