News for hackers. Stuff that matters.
Re:An experienced Javascript developer’s account of learning... by jasim on Saturday April 22, 2017 @10:10AM (#14172583)
If your web-app has even a modicum of complexity on the client side, the vanilla DOM based solution is going to get unwieldy real fast.

Consider a simple list of elements to which you can add or delete items. With vanilla DOM, you’ll have to write handlers that will `$.find` the particular element, and then run a `$.remove` to remove it. To insert something, you’ll have to do a manual `$.appendHTML`. And when you have to send the data over to the server, you have to iterate over the elements, collect it into an array and send it across. But if you used something that had an explicit notion of the application state, and allowed you to express your view as a simple function of your state, all you would have to write is an `array.push` and another `_.remove(array)`. The view will update automatically because we’ve already told the 'over-engineered' framework what to render for any given state.

But you could avoid framework and still not fall prey to spaghetti DOM manipulation. All you need is to keep an explicit state variable and have a single `render` function to deal with the DOM. This method will be invoked everytime you change your state, and it'll recreates the necessary DOM from scratch. It is a simple straight-forward implementation, but now you're going to lose cursor position, and with large enough DOM trees, the lag will be noticeable.

To fix this, you can add a virtual-dom implementation that'll maintain the DOM tree in memory and avoid re-rendering things that haven't changed.. and then you can have some sort of immutability checker to skip even the virtual-dom rendering part. And by now you have a home-brewed over-engineered React-like framework, but without any of its documentation or robustness or community.

We definitely need to be conservative in our choice of tools, but React today is a conservative choice to build rich front-end web applications. More than a framework, it is an approach to user interfaces, and it is a damn good one too.

Re:An experienced Javascript developer’s account of learning... by DenisM on Saturday April 22, 2017 @10:23AM (#14172645)
To be fair diffing virtual dom is not the only way - you could also be diffing the model and only update the parts which need it. Knowing the model well allows you write an efficient diff. The problem of course is that dom diff is universal while model diff is specific to application.
Re:An experienced Javascript developer’s account of learning... by lukaszkups on Sunday April 23, 2017 @03:03AM (#14176936)
or operate on document fragments
Re:An experienced Javascript developer’s account of learning... by olavgg on Saturday April 22, 2017 @11:04AM (#14172864)
No adding/removing/modifying elements in a list is very simple and fast, even with thousands of elements.

Example: https://pastebin.com/Fu39i47z

Re:An experienced Javascript developer’s account of learning... by drdaeman on Saturday April 22, 2017 @11:42AM (#14173034)
The good part about React is that given a `messages` data structure and a `render` function, it implements `addMessage`/`addMessageBefore`/`updateMessage`/`removeMessage` on its own. Writing once and not multiple variants of the same is good. Another good thing is that that there are a lot of components someone already published, that are ready to use (immediately, or after a few small fixes/PRs).

The bad part is that React has a lot of code on its own, and somewhat strongly suggest use of even more. It's orders of magnitude more than that your `{add,update,remove}Message` snippet has. While React tries its best to hide all that complexity, it's still there. Designing, writing, reading, debugging and rewriting (if necessary) those few functions is all trivial (but somewhat routine) - not so much with React.

Re:An experienced Javascript developer’s account of learning... by platz on Saturday April 22, 2017 @12:32PM (#14173267)
all the add/remove list stuff can be implemented by a simple templating library (handlebars/mustache) which doesnt mess up the whole client side architecture
Re:An experienced Javascript developer’s account of learning... by drdaeman on Saturday April 22, 2017 @12:51PM (#14173384)
Oh, no, I'm sure that's incorrect.

Templating libraries can't update documents, they can only overwrite them completely. This either loses state (entered-but-unsaved text, cursor positions, <detail> collapse status, etc.), or requires a lot of watchers and extra state information.

Those DOM updates is the whole point of React.

There are other libraries that work in a similar fashion, but React has more traction thus reusable components and recipes available...

Re:An experienced Javascript developer’s account of learning... by platz on Saturday April 22, 2017 @01:02PM (#14173441)
no, the point of the template engine is not to push all the logic into the templates. as you say you still handle the state yourself. the template render just skips the messy output building step, which for many cases is all you need
Re:An experienced Javascript developer’s account of learning... by drdaeman on Saturday April 22, 2017 @04:26PM (#14174554)
Ah, I think I see.

React is a templating library. Nothing more, nothing less.

It takes a state ("props"), a template ("component") and renders the DOM tree. That's all it does. (Non-templating uses like react-router are kind of hacks, rather than intended use. YMMV, of course.)

However, it's more complex than previous-generation templating libraries (that had emitted strings, rather than operating on DOM trees), because DOM trees are stateful. This is why React is so huge, when compared to, say, Mustache.

(And, of course, it's really easy to mess things up by putting application logic right in the component class. And, I'd say, sometimes it's really tempting to do so. Mustache/Handlebars doesn't have this only because they're specifically designed to disallow such things.)

Re:An experienced Javascript developer’s account of learning... by platz on Saturday April 22, 2017 @06:17PM (#14175149)
no disagreement on what react is. yes, I understand react can be used just to template (no one uses it this way). yes, react is powerful. yes, react provides a good mechanism for handling state and events. what i am saying though is that for some apps, you can get by with simpler templating and avoid the conplexity/power react gives you, handling state and events yourself manually. does it sound crazy? if you know it will work for your particular use case, i dont think its crazy, and you avoid a bundle of complexity that react gives you, in essesnce you own the complexity now
Re:An experienced Javascript developer’s account of learning... by drdaeman on Sunday April 23, 2017 @09:01AM (#14177986)
> react can be used just to template (no one uses it this way)

I don't know about the others but I do. Got bad experience when I've shoved logic where it didn't belong, don't repeat the same mistake again.

As the templating "got data, want a form, a table and a chart" engine it's good - exactly in a sense that I can write one template instead of multiple partials (add+update+remove). The complexity hidden underneath doesn't strike my fancy, but... it works, and saves coding time - especially because a lot of components are already written by someone else and is available on npm.

Still, it's really really easy to shoot yourself in the leg. And I found a fair share of tutorials and examples teaching those sort of things.

> react provides a good mechanism for handling state and events

No, it doesn't have those. Unless you count react-router/Redux/Mobx/whatever as React-the-umbrella-naming.

Well, there is some basic internal state management (via constructor + `setState` + `componentWillReceiveProps`) but that's about it, and IMHO it best very used conservatively, for approximately the same reasons why having logic in, say, Jinja2 templates isn't the best idea in Python world. Or maybe I just don't know how to cook stateful components properly, but last time I made a stateful component I ended up rewriting it some weeks later, extracting the state away, because it became a problem.

There is no event handling at all - except for the "props are changing, this is going to update" event, of course. All React has is `onClick`-like attributes and handlers bound to those are best left as some dispatch calls to the outside, with no logic inside the React component code whatsoever.

Re:An experienced Javascript developer’s account of learning... by zepolen on Saturday April 22, 2017 @02:01PM (#14173741)
I don't think you understand what OP is talking about.
Re:An experienced Javascript developer’s account of learning... by platz on Saturday April 22, 2017 @03:07PM (#14174157)
I think you're expecting a simple templating engine to do more than I'm claiming it's doing.
Re:An experienced Javascript developer’s account of learning... by csallen on Saturday April 22, 2017 @11:34AM (#14172998)
jasim isn't complaining about performance, but about the complexity of the code.
Re:An experienced Javascript developer’s account of learning... by valarauca1 on Saturday April 22, 2017 @01:01PM (#14173435)
Parent's paste bin is ~40 lines of code (with white space) consisting of 4 functions, with what? 4 loops.

If you consider 40LOC complex I don't understand why you work as a dev.

Re:An experienced Javascript developer’s account of learning... by jasim on Saturday April 22, 2017 @02:11PM (#14173811)
> If you consider 40LOC complex I don't understand why you work as a dev.

Goodness, your snark that questions the parent's competence is not only unwarranted, it is based on an incomplete understanding of the value of abstractions in programming.

It is not about 40 lines of code, it is about a basic programming model that allows us to understand and create user interfaces irrespective of how complex or large it is. It lets you compose your UI based on a tree of state and pure functions. If you don't have this abstraction, you do not have a coherent programming model to build UIs.

Re:An experienced Javascript developer’s account of learning... by drdaeman on Sunday April 23, 2017 @09:13AM (#14178016)
Actually, I wonder if there's something out there that has all the declarative state-to-DOM manipulation goodness, but does this at build time.

React is a runtime that works on the client, and its compile-time stuff is just syntax sugar (JSX) and some minor optimizations. As I understand, this runtime is the complexity that some complain about. At least I perceive this as a downside.

If there's something that can, given a component definition, generate those sets of add/update/remove functions and appropriate calls on state updates (preferably, as human-readable as possible) - I think that would be interesting to see and compare.

(I hope no one normally dynamically mutates their components at runtime?)

Re:An experienced Javascript developer’s account of learning... by jasim on Sunday April 23, 2017 @11:03AM (#14178420)
https://svelte.technology/ is what you're looking for.

I haven't ever used it because I find React quite adequate. I share your concern about the complex runtime layer that React introduces, and it is getting even more complex with the introduction of Fiber.

However, the test of an abstraction is whether it leaks undesirably, and whether it lets you escape to the lower layers when you need to. React scores well on both counts.

So while the implementation might be complex and will get even more elaborate over time, the abstraction is as water-tight as I can hope. In that case complex implementations with better performance might even be a good thing.

Also, I strolled through the React codebase in its early days and found it eminently understandable. So if it ever comes to it, I'm confident I can contribute a patch for the things I need with enough time and effort.