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.

Changing the class list appended to an element name causes the element to be removed and recreated

See original GitHub issue

I know the “correct” way to modify a class list is to use the class module, but I’m using snabbdom-jsx, and it provides a classNames property, which you can supply an array of class names to. The array is then translated to a period-delimited list of classes and appended to the element tag name, e.g.h('div.foo',...) becomes h('div.foo.bar', ...). As a result of this, snabbdom is removing the element entirely and relplacing it with a new element. This seems to me to be incorrect (not to mention inefficient) behaviour, seeing as only the class list has changed. I’ll log an issue with snabbdom-jsx as well and suggest use of the class module.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
paldepindcommented, Nov 18, 2015

When a virtual DOM library does children reordering it has to be able to somehow figure out which elements are identical. Snabbdom, React and most other virtual DOM libraries offer a key property to help guide this reordering.

I’m sure you know that.

With Snabbdom I decided that by default it would use an elements tag name, its id and its static classes to determine equality. This has the advantage that in many cases you can avoid adding key properties to elements and still do efficient reordering. Consider this example:

var vnode1 = patch(elm, h('div', [
  h('a.btn-primary', 'Click me'),
  h('a.btn-default', 'Click med')
]));
var vnode2 = patch(vnode1, h('div', [
  h('a.btn-default', 'Click med'),
  h('a.btn-primary', 'Click me')
]));

Here Snabbdom can only make a distinction between the two a elements by relying on the static classes. Thus it can efficiently do a reorder without needing users to supply a key property. This relies on a distinction between static classes and dynamic classes.

Thus I think this is neither incorrect nor inefficient behaviour.

We could do what @yelouafi suggests and add support for arrays in the class module. I would be open to do that. But I am not sure if it is a good idea. Determining added and removed elements between two lists take O(n^2) time and thus it would be less efficient than the current approach of using an object.

0reactions
yelouaficommented, Nov 21, 2015

Finally i think my proposition doesnt make much sens. It doesnt make sens to check all elms for an array to endup just applying the whole array to the elm. One could just use the className prop. So i think keeping the current approach as is betterm

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to remove an appended element with Jquery and why ...
I'm glad I could help. The jQuery best practices change all of the time when it comes to registering event handlers. It used...
Read more >
Element.classList - Web APIs | MDN
The Element.classList is a read-only property that returns a live DOMTokenList collection of the class attributes of the element.
Read more >
Update expressions - Amazon DynamoDB
An update expression specifies how UpdateItem will modify the attributes of an item—for example, setting a scalar value or removing elements from a...
Read more >
Context-Aware Web Components Are Easier Than You Think
An aspect of web components is that a JavaScript function is called whenever a web component is added or removed from a page....
Read more >
Classes | Webflow University
Use classes to save styling information that applies to as many elements as you want ... you can see the class name appear...
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