by mythz · 10y ago · 0 comments
But after using JSX I'm never going back to a templating system. JSX has an intuitive direct mapping to JS that lets you use the full power of a programming language without needing to learn another crippled DSL syntax.
If you've never used React before after reading this you could be forgiven for thinking it's a complex monstrosity needing a week of prep to build a HelloWorld app which is a biased gross over exaggeration. React has one core concept, Components, which is easier to create, encapsulate and reuse better than anything else I've seen. I also believe in React's being able to use JS for Views, Style and Behavior making it easier to encapsulate cohesive Components.
I consider Ecosystem, community, dev resources / backing, encapsulation and reuse integral for adopting a framework which React wins easily on but is quickly disregarded here.
Whislt this is a biased guide Vue has convinced me enough to evaluate it, I'm especially interested in its performance and being able to reference it like a jQuery script tag which I believe is the biggest shortcoming of modern JS fxs which has succumbed to npm micro module madness inflicting complexity and friction on all developers which is IMO the major source of complexity when adopting a modern fx.
Not only does it mask the fact that the supposed non-programmer template designer would still have to understand HTML and CSS yet somehow not have even the most basic knowledge of JS, but they would still have to be able to use whatever build tools are necessary to actually render those templates, including version control and node.js (which is the de facto platform for client-side build tools these days). The target audience seem to be old-school "webmasters" who FTP their PHP templates to the live server yet we're talking about technologies requiring a workflow that already precludes them.
So we're talking about a scenario that only benefits people who already tick all the boxes for "front-end developer" except they apparently suffer from a nervous breakdown if they have to look at an if-block or understand a for-loop yet they'll be fine learning another template language that is typically based on JS expressions anyway (though Angular was guiltier of this than Vue).
Maybe these people exist. Maybe there are teams where optimizing technical decisions for these people results in a net benefit. But I strongly doubt that this scenario is so widely applicable that this design decision makes a framework inherently superior when the cost of JSX to a team of JS devs is practically nil.
HTML is a Markup language. I get those.
CSS is stylesheets and in my opinion very straightforward. The hard part is making designs that are functional and beautiful and making it cross browser compatible, but CSS has very few warts if the "wtf" variety
JavaScript and it's ecosystem is disgusting for me. It's not hard, I can write stuff in it, it's ugly. I just don't want to touch it if I can avoid it. Pure personal preference. Typescript and elm help me to minimise the work I have to do in that mindspace. Maybe vue aims at similar clientele with less taste for statically typed/functional programming
The stated goal of making templating accessible to programmers isn't to shield people who don't like JS, it's to shield non-programmers who don't understand programming languages. This is the absurdity I'm arguing against.
If it was just about making JS look different, there would be no need to use Vue in the first place.
The message is something like: "These concepts you understand and use all the time? We aren't going to make you abstract them away. We're going to use them as the foundation for a new way to build apps."
I've seen this reflected strongly in large companies on the east coast. Design staff is expected to know HTML/CSS, but aren't exepected to have more than a cursory understanding of javascript.
Design will often start with HTMl prototypes, then do a handoff to engineering who will using a JS framework to wire it into the real app.
Lately, though companies are swithcing to React, because JSX seems similar enough to HTML that people presume that design staff will be able to understand it.
(but I'm also working on a long Angular 1 project at my job, so I "may" be biased)
I REALLY like that Angular 1 allows you to simply make a mockup, then add the "programming stuff" behind the scenes. WITHOUT an enormous build chain (I have enough of that on the server side already, and the server is not Node for historical reasons - I'm not sure about node yet, and even if I wanted it, that just won't fly in my group)
I'm not done yet, but I'm doing a presentation on this in December at one of the local user groups: https://github.com/roboprog/ang-prog-enh
There are companies with stupid silos like this, but for the reasons I laid out I doubt they actually benefit from the "toy-language templates" approach -- even in the aforementioned company the non-developer designers would implement the entire layout as a mockup in HTML and then toss it over to a frontend dev who would cut it up and add the actual template logic (because although the template language was "easy" non-programmers couldn't be "trusted" to get the logic right).
I see Angular's directives as a pure convenience compared to most every templating engine I've used across languages, never considered it something that makes it accessible to non-programmers. I'd actually think someone who only knows html/css would be more thrown off from a page with random, unsanctioned (by W3C), tags and attributes that may or may not reveal what they actually do.
As a programmer I don't want to write long templates, I seriously prefer Jade/Pug for that reason. When I can't use them for a client I typically use them to do all initial work, compile them to HTML, then make whatever minor changes from there.
Consider ng-for:
<div data-ng-for="y in x">
<h1>{{y.title}}</h1>
</div>
The ASP.Net solution is a pain, a function with HTML string template to dynamically add elements to a "runat" div.This PHP example seems more idiomatic, at least to me, yet takes more to write
<?php foreach($x as $y){ ?>
<h1> <? echo $y->title; ?> </h1>
<? } ?>
The JSP style is kinda similar to Angular, again maybe more idiomatic at first glance but still not as quick/simple to write <c:forEach items="${x}" var="y">
<h1>${y.title}</h1>
</c:forEach>
The jQuery/plain JS solution, especially before ES6 is not preferable by any means, especially beyond this simple example. var body = document.getElementsByTagName("body")[0];
[...].map(function(y){
body.innerHTML +=
"<div>"+
"<h1>"+y.title+"</h1>"+
"</div>";
});
Unlike all the other solutions, angular has 2 way data binding so when you programmatically add elements to the array in ng-for, they are added to the DOM without a reload.So, is the goal of a programmer to simply write more code? If so, then Angular is definitely not for programmers.
Is the goal of a programmer to solve problems? If so, Angular is definitely a tool for programmers.
{x.map((y) => (
<div key={y.id}>
<h1>{y.title}</h1>
</div>
))}
Aside from the HTML-like syntax this is vanilla JS. And "y.title" is already auto-sanitized so you don't have the XSS vulnerability as in your jQuery solution.Need to output trusted HTML?
<h1 dangerouslySetInnerHTML={{__html: y.titleHtml}} />
Yes, that's intentionally verbose, making you aware that this is a potentially dangerous. But to paraphrase the old saying: make the safe things easy and the unsafe things possible.Angular and friends require context switching. Good luck if you're using non-standard data structures or need slightly more complex filter or iteration logic. And especially in Angular (1 more than 2) you also need to keep in mind the variable scope within the DOM tree and which variables, directives and components you can use where.
With JSX it's just JavaScript and the scope is just your local JavaScript scope. Instead of having to remember all the idiosyncracies of the framework's own implementation of a for-loop[0] you just have to know JS.
I'm not saying Angular is not for programmers. I'm saying the design goal of Angular1 was to make it easier for non-programmers to write views/templates. It's even in the name: as much fun as controllers, components, directives and two-way bindings may be, the raison d'etre of AngularJS resides between the angle brackets.
[0]: https://angular.io/docs/ts/latest/api/common/index/NgFor-dir...
<h1> <? echo $y->title; ?> </h1>
You forgot htmlspecialchars and now you have XSS. Which is why facebook built XHP originally:https://www.facebook.com/notes/facebook-engineering/xhp-a-ne...
<?php foreach($x as $y): ?>
<h1><?= $y->title ?></h1>
<?php endforeach; ?> <? foreach($x as $y): ?>
<h1><?= h($y->title) ?></h1>
<? endforeach ?>
You just want to make sure to escape all output with htmlspecialchars, or define a shorter function like I did ("h").But you are right, i am not sure to use a template system again. But the advantage is cleaner code, many people change quickly to spaghetti code in jsx (mix all together, ifs, loops, ...).
For the record, JavaScript can not be easily refactored, since it's a scripting language with no strong types and no compilation phase, so to conduct a complex refactoring you will have to load all code base in you head and that's far from being easily. TypeScript for example is another matter.
Also, no amount of compiling or strong typing is a sufficient alternative to regression testing when it comes to refactoring.
It looks like you can use JSX with Vue: checkout the code for the benchmark mentioned in the article:
https://github.com/chrisvfritz/vue-render-performance-compar...
Yes. No doubt.
> which React wins easily
I disagree. The old react docs were shite compared to Vue's. Not once did I feel the need to ask google. (Have yet to go through the ones released yesterday)
The tools that the team provides (router, vuex, resource, cli, ..) work perfectly.
Where react clearly wins is with 3rd party libraries and learning resources. Egghead is a perfect example.
Depending on what you prefer one might win over the other, but it's not _clear_ by any means.
> https://www.google.com/search?q=React.createClass+versus+ext...