256 points · by wanderer42 · 10y ago · 147 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.
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; ?>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.
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...
So what's the pain for me, as a user of React? It's just embedded HTML for me. They never go on to explain why.
And...
Instead, we offer templates as a simpler alternative:
<template> <div class="list-container"> <ul v-if="items.length"> <li v-for="item in items"> {{ item.name }} </li> </ul> <p v-else>No items found.</p> </div> </template>
Notice how the templates are code in strings. I don't find that simpler at all. I find code in strings to be an anti-pattern. I already have a nice real language (TypeScript in my case) to do "templates".
After a while it felt like I was writing strings in strings in strings. And that was exactly was I was doing. I think JSX is a very sane way of coding after all. I think writing things like
<ul>
{childItems.map(x => RenderChild(x)}
</ul>
is wonderfully clear.But when it comes to css, vue's css are way better than React's alternative by a longshot.
The build requirement, and all of the tooling and complexity that is required to make that work.
There is no syntax to learn, except that you can have v-if and v-for in your html elements. A style block contains your CSS like you're used to, plus it can be scoped to a component. A script block contains a bit of local logic and event handlers.
Component = <template> + <script> + <style>. Give it a name with the .vue extension. Now you can use it like a custom HTML tag.
(If you really want JSX you can use it, too. Personally I'm migrating back from JSX to the above way of composing things)
Once you reach a certain complexity threshold in your app, you'll read how others solve this with a "flux architecture", include vuex in a few hours, and have these issues sorted out without hassle or nitpicking dozens of tiny libraries.
Need more "pages"? vue-router is there. Ajax-stuff? vue-resource just works.
Basically there are just 3 common issues left for Vue to become the ideal frontend solution IMO:
1) "official" bootstrap components that play well with vuex
2) a polished "react native" that just works, maybe soon solved by weex
3) SSR without hassle, ideally with a express.js middleware or something like this.
<p data-bind="if: foo">foo!</p>
quickly turns into <!-- ko if: foo -->
<p>foo!</p>
<p>
because this also depends on foo and I should
keep it DRY and adding a parent element just
to contain that "if" is silly.
</p>
<!-- /ko -->
Then you start to get comments everywhere and sometimes they don't match and you write server-side helpers to contain them then it becomes a soup of html, your server side templating language and knockout annotations.JSX is way cleaner.
We are still probably going to migrate to Vue.js though because it will be easier. (We love Knockout but having performance problems on edge cases.)
Doesn't vue-strap work well with vue? https://github.com/yuche/vue-strap
They start off saying Polymer is based on web standards, where Vue implements it's own system. They consider this favorable for Vue, because on -some- older browsers Polymer needs a polyfill to work. This makes no sense. The idea is that in modern browsers Polymer leverages the browser implementation of custom elements so it needs to do way less work. They completely ignore the fact that the Vue implementation needs to do work to implement encapsulation on every browser, even modern ones.
One of their points is that Polymer is stuck with plain CSS and JS, which is clearly not true, considering TypeScript and ES6 are the most common JS flavors in the Polymer ecosystem. You can use any CSS and JS framework or preprocessor you want with Polymer, you just have to tweak your build pipeline a bit.
And then at the end they claim 'It is also totally feasible to offer deeper integration between Vue with Web Component specs', except that it hasn't been implemented yet and they are waiting for the spec to mature (even though all major browser vendors have committed to the Web Component v1 spec).
So if something is technically (remotely) possible to do with Vue, it's worth mentioning as an advantage of Vue, but if something doesn't come out of the box with Polymer it's clearly worth mentioning as a negative?
I don't have any experience with the other frameworks in this comparison, but after reading the Polymer snippet I don't believe in the objectivity of any of these comparisons.
Shaving milliseconds in rendering, repaints, scripting, etc? For what? Modern devices make it almost irrelevant. If you'd be selling seconds to milliseconds I'd say ok let's have a look.
Complex dependencies vs zero dependencies. So now I need some library to bind/transpile/build many other libraries that in the end will...shave some milliseconds. You just added unnecessary complexity to solve the same problem, not to mention more KB to download.
JSX??? Don't get me started here.
Backbone core library is all I need. It packs routing, binding, event handling, rendering. Yours is useless out of the box, to be used on a real complex app it needs...more dependencies.
Fork Backbone, add a virtual dom to shave them milliseconds, and I'd argue that we've made progress.
A todo list on an mbp, yes. A complex app on a tablet, not so much.
> Yours is useless out of the box, to be used on a real complex app it needs...more dependencies.
All Vue requires is vue.min.js and you're set. Adding routing and clean data flow out of the box is trivial. _But_ it is easy to end up with a massively over engined babel-webpack-flow-npm-flux meme stack, given the excellent vue cli. I am, of course, guilty of that myself.
I also like my components to be tightly coupled. Having a template just for a single component is not useful I learnt that from my backbone js experience.
Declarative syntax is very good win for reactjs. Most can eaily understand the component code. It requires few basic life cycle to be learnt rest is just JavaScript.
I'm not familiar with the concept of simple js vs advanced js in react. But your statement about Vue's tags being its own dsl holds true for jsx as well.
On the flip side Native rendering will require learning yet another framework[2].
EDIT: You are not limited to using the templates, JSX is also an option[3].
[1] https://news.ycombinator.com/item?id=11270213
The concern over React's licensing terms is that:
1) Facebook might have a patent on some aspect of React (such as the virtual DOM).
2) You might get into an unrelated patent dispute with Facebook.
3) This would allow Facebook to terminate the patent grant.
4) A court might hold that the implicit patent grant usually assumed to be in the BSD license was superceded by the explicit patent grant.
5) Facebook can now sue you over your violation of their virtual DOM patent.
That's a fairly tenuous chain of events, but sure, it's possible. So let's say instead you pick Vue. Assuming Facebook does have that virtual DOM patent:
1) Facebook can sue you over your violation of their virtual DOM patent (since Vue also uses a virtual DOM).
That doesn't really seem better? Actually, it seems much much worse! Of course, I have no reason to think Facebook has any such patents, and if they don't, then the entire discussion is hypothetical. But still, it is interesting to try and figure out what patent they might hypothetically have which applies to React but clearly not to Vue or other similar framework...
It makes project not maintainable. So using plain JS is not an option for the large long term projects.
Why do you think I need to think in terms of components? I'd say you need to see a bigger picture not limiting yourself by only components approach. Components/modules is an essential thing of any complex project in general, ie not specifically related to the web development, but that's not enough. Try to realize that for example using praised by hipsters mixins for inheritance makes it very complicated to track down from where things came into the final object.
Ah Javascript programmers, you are a source of constant entertainment:
https://en.wikipedia.org/wiki/Loose_couplingPeople have pointed out here that it's easy to use JSX with Vue, and that could be something I might look at.
I appreciate what Facebook were trying to achieve with JSX, but libraries like Mithril serve as an example that native templates can be done without a syntax extension.
JSX is a new templating language too. It not standard; it not JavaScript.
Edit: I can understand shipping logic from the server to client, for validation perhaps. But... meh.
* 40% UI (visualising states, handling user input, feedback, etc),
* 55% moving data around (read an XML file, translate it, push it into db over there)
* 5% algorithmic work (given input of x million records, figure y out)
The UI stuff I already did in Javascript, however it turns out that one single threaded process can shuffle a lot of data around (resource efficiently), if I adopt an async programming style. I know NodeJS is not the only language that does async stuff nicely, but it's rather convenient to have the same language everywhere (though I recognise Javascript is not the most elegant).1) It makes no difference at all if is the server is implemented using a different language than the client.
2) You shouldn't share any logic/code between the server and the client, even if they are using the same language.
Javascript is not going to be faster than Go or Rust, which are increasingly taking over as web backends. (I'm writing stuff in Rust's Iron framework lately.) Or Java, for that matter.
I can serve more requests from these languages than "concurrent" Javascript.
2. If you are working on page/site that may or may not evolve into an SPA, choosing Vue.js over Angular or React seems like a no-brainer.
3. If you are working on an SPA from the the get-go, then the choice becomes more difficult. However, if the SPA is only one part of your site Vue.js offers you the opportunity to use it site-wide, whereas using React or Angular site-wide would quickly become quite cumbersome and I'd guess is most cases folks actually use React/Angular for the SPA pages and jQuery for all others.
As an anecdote, I rewrote a large site from jQuery to React without precompiling my JSX and using ES6 via Babel. It took 3 script includes and then I could do a gradual transition and replace elements with React instead of jQuery. jQuery components lived happily alongside React just fine. Even though I did nothing to optimize page loads, I made massive gains for performance, even with React in dev/debug mode.
I wonder if we will ever have a satisfactory solution to the template problem of conditional rendering. All I've seen so far solved one problem only to bring another. To some degree, this is really a dogmatic problem, which is why it'd be nice if we could solve this and move on with more important things.
It's the same old balance problem: more flexibility and less constraints opens up potentially dangerous misuse.
We see it with languages too I.e C memory management vs garbage collection.
We also see it socially too. There are particular problems with both 0% regulation and 100% regulation, with the ideal that nobody can agree on lying somewhere in between. In fact, some situations call for more or less, compounding the confusion.
Perhaps the solution is a template system that can fit current needs, in that a programmer can choose to remove a restriction but the removal of the restriction gives the seconds required to think if removing a restriction is necessary.
That then means we can collaborate as an industry. We may find that there exists one or a handful of "ideal general restrictions". We may also find that actually for certain types of problem would do well with a handful of specific restrictions that work well there.
I just don't think there is a one size template solution and our attempts at a fixed ideal haven't seemed to work. After all, if we had the true ideal, we wouldn't keep making new ones.
JSX doesn't run in browsers.
This is incorrect. It's easy to use ES2015+, TypeScript and CSS preprocessors in Polymer.
I am sorry, but that looks totally wrong with the previous code example. Vue in that instance seems to be reinventing Javascript in HTML (while the critique is true that React reinvents HTML in JS).
Just randomly going through the Vue docs, we see stuff like:
>> Note that you should not use an arrow function with the data property (e.g. data: () => { return { a: this.myProp }}). The reason is arrow functions bind the parent context, so this will not be the Vue instance as you expect and this.myProp will be undefined.
I don't want to deal with this crap anymore. I'm still making nightmares about $scope.$apply. (Yes I know Vue doesn't have the $apply problem, that was just an example).
Where I think Vue.js really shine is for teams that want a standard, industry proven solution, to build web apps. Similar to Angular, you don't have to re-invent the wheel. You just look in the documentation and use whatever is built for you. I think this is great for many projects. Personally I've been burned by the limitations of those frameworks and I'd rather keep my freedom to build things the way I want.
In the Vue comparison, they say this is how you build components with React:
render () {
let { items } = this.props
let children
if (items.length > 0) {
children = (
<ul>
{items.map(item =>
<li key={item.id}>{item.name}</li>
)}
</ul>
)
} else {
children = <p>No items found.</p>
}
return (
<div className='list-container'>
{children}
</div>
)
}
If you're the kind of person who keep repeating that kind of code everywhere in your app, then yes, maybe Vue.js is better for you. But such if/else pattern is easily abstracted away with a simple function which makes it as concise as the Vue snippet without relying on directives in templates. Also, keep in mind that this is only one use case. What if it needs to be slightly more complex? WIth Vue/Angular, you're forced to add directives or whatnot. With React, same render function. I like that cognitive simplicity. Yeah, maybe the render function is slightly more complex, but at least there's only that to be aware of. Not hundreds of directive/filters/whatnot spread everywhere.In a way, I feel like React is more lower level. I use it to build my own framework on top of it. Where I feel like Vuejs, similar to Angular, is the framework.
- Blaze, meteor's original view layer, lacks components and has very slow development.
So, comparison with meteor quite useless.