Lurker News

Comments (156) 28 threads

Earlier comments
sfvisser1 reply#
Nice! Explorations like this are truly useful for understanding and maybe even for innovation. You might figure out new ways of expressing things that are unlike react, but solve particular problems better.

As a next step I'd recommend trying to add component-local state connected to the component's lifetime. This is essential when building larger applications out of abstract building blocks that hide inner workings. I expect this to be a nontrivial addition, but I might be wrong!

leontrolski#
I'm not sure I can quite express what I'm thinking, but do you think this could be achieved orthoganally to the rendering library itself? (Just by "normal" js).
joosters6 replies#
This is probably a naive question, coming from someone who has written very little JavaScript:

Why do we need React? Why can’t the browser do the same job? Instead of passing a virtual dom to react, and letting it ‘diff/patch’ the real dom, why can’t code regenerate the dom directly, in a similar way? This would seem to save both time, memory and effort, since the browser can do the diff/patch itself, much more efficiently?

quezzle1 reply#
It’s a framework.

Similar questions are:
Why not build rails into ruby?
Why not build Django into python?
Why not build qt into c++?
It’s a programming flow and model, dom diffing is just one part of it.

ken1 reply#
Those examples are all programming languages.

"Web browser" is not a programming language. It's practically a complete operating system, and they have not in the past shown much reluctance to add features to that big ball of mud, when it helps programmers.

galaxyLogic#
Right. I think the value of this "React in 33 lines" article is it shows us such an API would be easy to implement and add on by the browser-makers.

Would it be as fast as React? Well it wouldn't need to be, but it well could easily be since it would be implemented in native code.
It could be an extension to the Shadow-DOM API, right?

masklinn#
Because the DOM is a very specific style of API which is both extremely procedural and extremely heavy, your average DOM element is full of cross-links to other elements, so generating a DOM is both a pain in the ass and quite expensive. It's also extremely stateful, so while you could generate a new DOM and replace the old one you'd lose all information in flight e.g. focus.

Browsers might be able to provide a lighter vdom layer but… well first I'm not convinced the gain would be that big, and second what would the API be? It's not like there's a clear winner that has emerged, react is quite popular but hardly the only contender, and react has kept evolving its API so the sort of stability you'd want enshrined in a browser isn't exactly there.

jfkebwjsbx#
Because browsers were initially meant for another job: displaying static, declarative documents.

Slowly, they gained more and more features and performance until people started creating single-page apps, where you need to re-render on state changes all the time.
So people invented the best ways they could do so within the boundaries of the browser: the diff/patch approach, in order to work around the performance issues of the document model.
And in some time, perhaps browsers end up standardizing a way to do this natively. Or perhaps another app-model rather than doc-model (doubtful). They already tried to start with templates and whatnot, somewhat unsuccessfully.
In summary: the document model (and browsers) were not designed for that. The Web is a mess of features and features on top of features that has ended up with roughly a secondary operating system on top of a real operating system. The only (but critical) advantage of this model is that it is a de facto standard.

redis_mlc#
Vanilla-js. Werd.
pier251 reply#
I've been saying this for years.

If reactive data and data binding were solved at the browser level we'd save so many kBs and CPU cycles.

efdee1 reply#
If my cat was a cow, she'd give me milk. The hard part is turning my cat into a cow.
Silhouette1 reply#
I'm not sure this is a great argument. Web standards and browser implementations evolve partly through absorbing ideas that lots of people are using anyway. A decade ago, no-one was using CSS variables or HTML media elements, but lots of people were using CSS preprocessors and Flash video players.

Continue this thread — 1 reply

Silhouette#
The big advantage of a library like React is that you can specify your UI declaratively once, instead of having to specify imperatively how to handle every possible state transition. This can dramatically reduce the number of different rendering cases you have to write if you have a lot of state to manage.

All the virtual DOM stuff is just an implementation detail that makes the declarative stuff fast enough to use in production. A more naive implementation that rebuilt your entire (real) DOM on every state change would not be fast enough for anything but the most simple applications, because DOM updates in browsers are currently a relatively slow operation.
Browsers could provide an API to more efficiently update large sections of the DOM at once, using a React-style diff algorithm or otherwise, but currently that isn't a feature they offer. If they did, you'd be correct that there would be less need for libraries like React.

peey#
It seems like `m.update` function queries the current DOM state.

I'm interested in the performance implications of this. From what I know, other vdom libraries maintain the current DOM state in-memory for efficient computation of what should be updated.
No doubt that this is much simpler and ought to be at least more efficient than re-drawing everything. How much performance do you lose due to querying the DOM state?

manthideaal2 replies#
In the source code to perform el.classList = v.classes the author uses:

  for (const name of v.classes)
    if (!el.classList.contains(name)) el.classList.add(name)
  for (const name of el.classList)
    if (!v.classes.includes(name)) el.classList.remove(name)
Why the standard decided to make classList read-only?

Could ele.className=(v.classes).join(" ") be a valid and performant solution?, perhaps is to avoid the string to token traslation for performance reasons?, then why don't they include a classList.set method?

leontrolski1 reply#
Agreed, let me know if you find a better way! I feel if the browser APIs were slightly more declaratively inclined, this article could be "10 line React".
masklinn1 reply#
> Why is classList read-only, I can think of any valid reason.

Because it's a live DOMTokenList, not a computed property. That is if you keep a reference to a classList and mutate the className parameter, classList will reflect the changes.
classList could also be a computed property which, when assigned to, clears the underlying token list and adds all elements but I'm guessing the developers of the API saw this as unnecessary complexity given you can do the same thing by setting className.
The point of classList is the ability to more easily check for and toggle individual classes, if you don't need that capability you just don't use classList.
> Perhaps ele.className=(v.classes).join(" ") is a valid solution?
Yes.

leontrolski1 reply#
Great answer, thanks! Embarrasingly, I didn't even know about className :-)
tonyarkles2 replies#
I’m not sure this is the right place, but... I see a potential subtle bug there too: it doesn’t seem at first glance that the classes get added in the same order as they’re specified in the virtual element? If I made div.bar and then changed it to div.foo.bar, properties of .foo could override properties of .bar because it looks like it would end up with classList [“bar”, “foo”] instead of [“foo”, “bar”]? Maybe?

Edit: please don’t take this as criticism. I love the project and think it’s awesome! That’s why I took the time to read through the source and grok it!

Continue this thread — 2 replies

Load more comments