r/javascript • u/magenta_placenta • Sep 23 '16
Why Vue (not React) is the new jQuery
http://anyonecanlearntocode.com/blog_posts/why-vue-not-react-is-the-new-jquery10
u/verletx64 Sep 23 '16
Easy to learn doesn't mean a lot, in and of itself. First concern has to be does it provide a solution for us, at the scale we need. Then we start looking at easy to learn, this, that and the other.
This is why the 'to do list' demo is perhaps so vilified in the community, it's mostly just used as a way to say 'hey look how few lines we can solve this trivial problem in' when in reality the business problems are much more complex and even the way we evaluate what a good framework is, is in my view, quite different as the size of the project increases.
20
u/gabbsmo Sep 23 '16
Can we please stop comparing jQuery with all these MVVM libaries and frameworks? jQuery is just a package of workarounds for browser quirks so we can develop for browsers and keep our sanity. Unlike most other tools for the web it actually becomes smaller over time as the web platform stabilizes.
18
u/masklinn Sep 23 '16
That's not quite true, jquery was both a cross-browser system papering over browser incompatibility and a terse, powerful, fluent, set-oriented, high-level DOM API trying to make good use of and provide good integration with its host language rather than provide for some unholy lowest common denominator of java, c++ and JavaScript.
1
u/benihana react, node Sep 24 '16
Unlike most other tools for the web it actually becomes smaller over time as the web platform stabilizes.
which when you think about it, means babel is actually the new jquery, not react.
21
Sep 23 '16 edited Sep 23 '16
This is quite deceptive. It looks simple in the beginning, it won't be once the project grows. Mutating state in data will lead to so many problems down the line you probably will use VueX anyway at which point you're already close to Reacts structures. Messing around with template strings will have you study manuals and gotchas before you know it. Vue's api, lifecycle and state management is actualy quite complex.
React on the other hand looks scary. If you learn it you will quickly reralize it is way, way easier than Vue. React has a handful if api's of which you need one in the beginning: renderElement. The rest is about structuring a class, willmount, didmount, render. Now you know React. Often Reduxes complexity is brought into this, and it makes it all look even scarier. But it isn't even specific to React, it just teaches you how to write apps that are easy to reason about and how state flows through it.
12
Sep 23 '16
there's nothing inherently bad about mutating state; in fact it can be incredibly useful to use mutable datasets (simple example: do you really need an immutable object for internally tracking a method's implementation?) the problems come from shared mutables, where there's no clear indication of where changes can come from and debugging devolves into a giant game of whack-a-mole.
8
Sep 23 '16 edited Sep 23 '16
Mutating state is fine. We fought against observable gotchas like not being able to create or set new objects, "leaking" state where the tiniest mistake would result in Vue climbing through your objects transforming them into slow observables, and state management where there's simple no indication where to put local state that doesn't want to be auto-transformed. There is a lot of "magic" going on with unexpected side-effects, unless you study the docs, and even then things aren't exactly clear. This is something you don't have to do with React because it's quite clear how it works and what it does.
1
Sep 23 '16
naive question, since i've used vue but not on anything enterprise-y: how much would mutability guards (whether as robust as ImmutableJS or as simple as _.clone) help with your situation? granted, that would be the clearest example of fighting the framework you could get, but if it's an option then it'd be handy to know.
4
Sep 23 '16
Vue's api, lifecycle and state management is actualy quite complex.
Sincere question: what about it do you find complex? I'm currently working on a not so trivial application with Vuex and everything and it's working out quite nice. Being a backend developer, I actually really liked it that it was easy for me to get started with Vue and then gradually evolve into a more structured application.
If I had to describe Vue, I would say it's between jQuery and React, depending on which way you want to pull it.
2
u/staircasebug Sep 23 '16
You mean Angular and React? I get the author's comparison to jQuery to a certain point as far as ease of use, but as a growing fan of Vuejs, making loud literal comparisons of it to jQuery is leaving me SMH.
Agreed on Vuex. Everything is nicer with Vuex/Redux.
1
Sep 23 '16 edited Sep 23 '16
You need to know how observables work and what they are. Why you can't create new objects in them, or replace them. Then you account for it with specific $setters in objects and arrays. Or use VueX from the get go. You need to know that it transforms everything in this.data and that it is not a place for local state that doesn't want to be reflected. Put a single THREE.js object in there and it will transform the entire scene graph down to the buffer arrays (this happened to us). But now, where to put state? You don't have a clear component or class with a constructor. You have module scope, which is dirty, or adding your stuff into this.pleaseDontTouch, but under circumstances it still gets transformed (add it in a specific component callback, there's an issue where Evan explains when).
React does not have any of these issues. You have a clear component. It has a constructor, put anything you want in there, it won't mutate. It has a reactive state, where you put reflected data. It has a callback for mounting, dismounting and rendering. You don't need to study a template language with gotchas of its own, you just use what you know. It can be composed naturally without extending or prepping other components. You don't mess around with slots, you just filter or iterate over the components children. I don't know a single case where Vue is simpler.
-5
u/phpdevster Sep 23 '16
Sorry, but React's differentiation between state and props, and unintuitive lifecycle hook names is not exactly simple.
3
u/z500 Sep 23 '16
Maybe I'm missing something, but I don't think it takes two paragraphs to explain the difference between state and props.
3
Sep 23 '16 edited Sep 23 '16
They're verbose, but we're talking three names here. As for state, it's a normal class, it can't be any easier. The component also has a state prop, just like Vue's data.
Question to you. You want to make an editor component using codemirror. Ignoring that there may be ready-made bindings for now.
<template> <div> My Editor <textarea id="editor"></textarea> </div> <template> <script> export default { ready() { let editor = CodeMirror.fromTextArea(this.$refs.editor); // ??? } } </script>Where do you store "editor" so it can be used later on?
React? I don't have to think about it, because it's regular Javascript. I know React won't mutate local state and i know what my class does and does not do. It's not just a stateless object as well.
class EditorComponent extends React.Component { componentDidMount() { this.editor = CodeMirror.fromTextArea(this.refs.editor); } render () { return ( <div> My Editor <textarea ref="editor" /> </div> ) } }1
u/Maklite Sep 23 '16
You basically do the same in Vue. You can use
this.editor = CodeMirror.fromTextArea(this.$refs.editor)in theonMounted()function and have it accessible everywhere else. It won't be reactive because you haven't specified it in in thedatablock.I feel Vue vs React is like an Apple vs Android or Mac vs PC thing. Both camps have their pros and cons and it's difficult to convert people someone once they're settled.
2
Sep 23 '16 edited Sep 23 '16
Under circumstances Vue will climb through. This happened to us.
See here:
https://github.com/vuejs/vue/issues/1988
https://github.com/vuejs/vue/issues/2637
The official way is using the created hook, only from then on it is guaranteed. This is from Evan himself:
{ data: ..., created: function () { this.myPrivateStuff = { ... } } }Some people have still seen it transform, though.
There's also a plugin that handles it once and for all, though it makes it quite verbose: https://github.com/rpkilby/vue-nonreactive
As for the camp thing. I actually love Vue and use it myself. The only sticker on my laptop is a Vue sticker. I use it with Vuex and that's fine. I also use React. But oftentimes people make it into something which it isn't. It needs a lot of study and learning to handle it right. It is great for using HTML structures you already have or styles. .
5
u/phpdevster Sep 23 '16
But that's the author's point - when you do ultimately want to go a flux-like route with your state management, Vue supports it. But for when you don't need that, and need only a smattering of UI behavior on a page, Vue supports that with less boilerplate (and more intuitively) than React.
It's a very flexible tool in that regard.
1
u/leeharris100 Sep 23 '16
I think that's kind of the point of the article. If you want a quick, small, or dirty project then Vue CAN replace jQuery as a drop-in "helper."
But once you start moving into large scale SPAs or something you can bounce between React + other stuff, Angular2, Vue + other stuff, etc.
1
u/pier25 Sep 23 '16
Couldn't agree more. Vue seems deceptively simple at first but once you start getting your hands dirty it becomes a mess IMO.
6
18
u/name_was_taken Sep 23 '16
I don't see that Vue example being any easier than just using jQuery for it. And I don't see it scaling in the way that React and Angular do.
In short, I don't see why people pick Vue.
18
u/staircasebug Sep 23 '16
I wouldn't judge Vue's ability to scale on that "Hello World" or TODO example.
A better example of Vuejs is the hacker news clone done with Vuejs 2.0 & SSR: https://github.com/vuejs/vue-hackernews-2.0
Just like one of the other comments mentioned, Vue + Other stuff is just as powerful as anything else. Take it with a grain of salt, but Vue's performance is near the top vs the others. https://rawgit.com/krausest/js-framework-benchmark/master/webdriver-ts/table.html
I've been playing with it for the past week or so and I like what I've seen. What Vuejs doesn't have going for it is the HUGE ecosystem of resources that React has generated with its popularity, but with the impending 2.0 release, I think I see that improving in the near future.
6
u/ataraxy Sep 23 '16
It was way easier for me to reason about how to do things with Vue over React. It fit my mental model better.
I also prefer handlebars over JSX, though now that's also a valid possibility in Vue if it's your preference.
6
u/Danmoreng Sep 23 '16
I am currently looking which of those 3 frameworks I want to learn, and Vue.js looks most interesting to me. It appears to offer the same functionality like React or Angular, while beeing easier to learn.
Tell me: Why should I learn React instead of Vue?
17
u/colbycheeze Sep 23 '16
I never get the "easier to learn" argument with react. It takes less than a day to understand 90% of react. If you know JavaScript and html... You know react.
2
u/FurryFingers Sep 24 '16
I think there's an implication of not getting easily tied up especially as development gets more complex - or bogged down in "gotchas"
3
u/staircasebug Sep 23 '16
In my opinion, you can't go wrong with either.
As I mentioned above, the resources available to React ecosystem are huge--you might find yourself spending a bit more time with Vuejs simply because a tool/plugin/component isn't out there as it would be with React.
The real hurdle is bending Webpack to your will and wrangling with the configuration. Scaling apps need Webpack's magic and this tweet is a fairly good summary of the current state of it. https://twitter.com/codeincontext/status/778580127416451072
8
u/name_was_taken Sep 23 '16
I love the way React packages everything about a component together. When I make a LiveVideo component, I can add a src property to it and just let the component take care of itself.
With Vue, as it looks in this example, I need to have all the HTML already in place and then it'll connect the events together. That's basically just jQuery with a magic syntax. After going after some better examples, it appears that Vue has its own component syntax and looks rather like Angular. (Obviously, this article chose a bad example.) It still keeps things split up, though.
React has an up-front cost of having to compile the code with webpack or something, but you get to keep everything together and it's easy to read.
In the end, it does come down to style. I still don't know why someone would choose Vue over Angular or Ember, since those are already well-known and there are lots of tutorials and community about them. Again, I think this blog post does a bad job of convincing people of their stance.
8
u/phpdevster Sep 23 '16
I love the way React packages everything about a component together.
Vue does that too with the use of .vue files. Read through the docs to see how.
Personally, I think packaging markup, logic, and style actually makes code LESS reusable and maintainable, but Vue lets you shoot yourself in the foot just the same as React if that's what you want.
2
u/menello Sep 24 '16
If packaging your markup, logic, and style together makes your code less reusable and maintainable then I'd dare to say you've done something incorrectly. Something might be too tightly coupled to something outside of the component which makes it harder to reuse.
1
u/phpdevster Sep 24 '16 edited Sep 24 '16
By packing your markup, logic, and style together, you are creating a tightly coupled and tightly structured widget that decides for itself how it will look, rather than letting its surrounding context decide that. I don't know about you, but not once have I ever built a site where a similar widget always looks exactly the same in every context.
The information might be the same, but the way it's displayed/presented can be radically different from one context to the next. What you want is to be able to re-use data and behavior, but then present it differently.
When you bundle markup, logic, and style together, you are effectively creating the equivalent of a Facebook friends widget that is always meant to look exactly like that no matter the context. That's great if you have a really basic GUI, but a complex GUI requires more flexibility than tightly coupled logic, style, and markup.
Even something as basic as a search field with live search-as-you-type behavior can look radically different from one context to the next. On the home page, it might need to be larger and more obvious, but on the search results page it can be less obvious (or vice-verse)
Attempting to turn GUI elements into composable lego bricks is extremely limiting. MAYBE it makes sense for something like an admin GUI, but nothing that's meant to be more elegant and nuanced.
1
u/addiktion Sep 24 '16
This is always the challenge I think. How isolated do you go.
In my mind if you pass a prop into a component and it changes its look it seems like the component should be responsible for the change of visuals. The outer context can say hey component you are a size=big now and the components internal styling does its thing for the home page.
I get what you are saying with tightly coupling but props are the public API interface to help alleviate that.
2
u/phpdevster Sep 24 '16
Props are for data injection, they don't really have much to do with styling and markup.
0
Sep 24 '16 edited Sep 24 '16
You still can't just go and compose Vue components. You either declare your component to be in the public Vue scope or you prep the other component so that its template may receive it through the this.components property. Not to mention that you're tied to slots. In React a component is packaged up, self-contained and can be used naturally:
Button.js
export default const Button ({ color, children }) => <div style={{ color }}>{children}</div>OtherComponent.js
import Button from 'Button'; const SpecialSpan ({ children }) => <span>{children}</span> const OtherComponent () => ( <div> <Button color="red"> <h1>caption</h1> <SpecialSpan>subtext</SpecialSpan> </Button> </div> )This is also the reason you find so many components and controls for it. You import them and use them.
2
u/rk06 Sep 24 '16 edited Sep 24 '16
Actually, you can do it with vue. You use components field in component configuration to register components locally to a specific component.
I am on mobile so I can't code it right now
1
u/luke3br Sep 24 '16
Yeah, it looks like you need to read the Vue docs before you assume it can't do that.
1
Sep 24 '16 edited Sep 24 '16
I know the Vue docs and i know it can compose. But it can't compose freely. If you want your template to accept other components you have to register them, so they're "visible" to all others in the session
Vue.component('my-component', MyComponent)or declare them locally
var OtherComponent = Vue.extend({ template: '<my-component></my-component>', components: { 'my-component': MyComponent } })You have to do this to overcome the limitations of the templating system. It certainly doesn't make things easy. The React example above imports a component, creates a local component, and just uses them as if they were regular objects, which they actually are.
1
u/luke3br Sep 24 '16
I might have misunderstood. Is this particular problem solvable using render/create element?
2
Sep 24 '16 edited Sep 24 '16
As of Vue 2.0 it can parse JSX and the example would indeed work, in a slightly more verbose fashion. Though at that point you're using React. Only that you're incompatible to anything in the React eco system like components, add-on and libraries, which next to JSX and the simple api is Reacts strongest benefit.
1
u/luke3br Sep 24 '16
To each his own I guess. Both are great frameworks IMO, and I have nothing against React at all. My brain likes Vue more than React, but that doesn't mean I don't use React sometimes.
Vue's version of React Native is extremely young, and I'm not sure if it'll ever get to the point where I'll use it instead of React.
4
u/Franks2000inchTV Sep 23 '16
I am a relatively inexperienced programmer and I've tried a couple frameworks and react is by far my favourite so far:
It's very easy to use. JSX is such common sense syntax.
There are tons and tons of resources. Libraries, tutorials, answered questions on stackexchange etc. The fact that it's so popular means that there's a ton of stuff out there to help.
Somehow, it just makes sense in a way that other frameworks haven't before.
3
1
Sep 24 '16 edited Sep 24 '16
It appears simpler but it isn't. It is a very complex framework with intricate internal rules and arbitrary templating syntax when you're beyond the todo example. It has gotchas and oddities about it that just don't seem apparant in the beginning but they'll come out.
React is simple. It has three api calls, of which in the beginning you need one (reactDOM.render). You need to know the lifecycle methods (componentDidMount, componentWillUnmount, render), a component has some properties (props, refs, children and state). This is React. If you have used it once you won't have to consult a manual, you just know it. The rest is about applying common javascript and realizing how ES6 can make the syntax simpler.
1
u/Graftak9000 Sep 24 '16 edited Sep 24 '16
React synthetic events did make me scratch my head for a bit. Another thing I'm not too keen on is the advocacy of dumb components yet smart components are not always avoidable (beyond root).
Oh, and it would be great if the key prop would be generated/injected automatically somehow.
1
Sep 24 '16
Ok, it was really weird to see the warning at the first attempt to make a debounce because the event was re-pooled. I remember that now. Though later on it's quite nice as it abstracts the browser inconsistencies. The key prop actually is auto generated (or indexed) when none is given but results in the scary warning. It has driven me into into using Math.random() or v4() guids, making children render every at re-compose, while a missing key would be faster. Found out about that one day. It does have its quirks.
1
u/MildlySerious Sep 23 '16 edited Sep 24 '16
Out of curiosity, did you get to look at Inferno? It seems to hold up as one of the fastest in the benchmarks, while being similar to React.
1
u/pier25 Sep 23 '16
Because Vue seems a very simple data binding library at first. It becomes muddier the deeper you go.
Vue came to fame because the Laravel community started using it. PHP guys...
4
u/phpdevster Sep 23 '16
This is why Laravel's popularity skyrocketed - it's insanely easy to get going with, but immensely powerful in the hands of someone who understands PHP and application architecture.
2
Sep 23 '16
i really like working with vue, but it wasent until i setup a proper gulpfile with gulp-inject, creating vue components, i started to enjoy it
3
u/_HlTLER_ Stackoverflow searcher Sep 23 '16
I get what the author is trying to say but he fucked himself over with that article title and insistence on mentioning jQuery.
He's trying to say that Vue is easy to learn and allows beginners to perform more advance things like how jQuery makes things easier by solving cross-browser issues.
He's not trying to say Vue can replace jQuery as a general purpose toolset.
3
2
u/vagol942 Sep 24 '16
jQuery IS the new jQuery, I mean the web is basically wordpress and landing pages not SPAs.
React + toys, Vue + toys and Angular are SPA frameworks and you use them to make applications that just happen to run on a browser, doing such things with jQuery is and has always been a clusterfuck.
Going back to the Vue vs React thing, they are both component base view rendering libraries, Vue is objectively easier to use, but at least for me the "no blackmagic approach" of React wins for me.
1
u/scootstah Sep 23 '16
jQuery eliminated many barriers to entry by NOT requiring a programmer to be versed in best practices, design patterns, etc
Eh. I don't agree with this portion. I mean, jQuery sort of had that effect. But more than that, jQuery allows you to do incredibly simple tasks without having to write loads of code. Stuff that should already exist in the Javascript standard library. And on top of that, it gave you a nice simple API to do those things - instead of the cruddy nasty standard Javascript API's.
1
Sep 24 '16
[removed] — view removed comment
1
u/scootstah Sep 24 '16
Sure I do. The JavaScript stdlib is laughably lacking. In some cases you're doubling or tripling the amount of code to do in vanilla js what you could do in jquery. That's one of the main claim to fame's of jquery - you can just get in and do simple things quickly and painlessly.
Remember that jquery was created before html5 really took hold, and before all of the modern library soup that we have today. It totally out shined its competition. Stuff like mootools and prototype couldn't compare. Today, jquery loses some of that, but it's still useful and I still like it for small simple things. If nothing else, I tend to use it for its awesome selector and node traversal, and really easy AJAX.
If I need a big framework then I'll use something else. If I just need to do some lightweight UI/AJAX stuff, then I might just throw jquery in. Half the time it's already a dependency anyway, so it's already available.
I spent about two weeks once optimizing a jquery interface - which usually meant dropping to vanilla js. So, I know its limits. It's not for everything, but it still has a place in my toolbox.
1
u/tubbo Sep 23 '16
I'm not sure I agree with this, as jQuery was also adopted by experienced developers who were tired of having to write nasty cross-browser code. It was simple, a lot lighter than Prototype, and got the job done. Furthermore, it had a really simple API that made programming with the HTML DOM a lot less aggravating.
jQuery really shined (and continues to shine) when your site doesn't need a lot of JS.
1
1
u/hallettj Sep 24 '16
Thanks for the post! I've been thinking about how to get people started with web apps. You have given me some things to think about.
1
u/dwighthouse Sep 23 '16
So... mostly redundant, with multiple versions of it on the same page, and an entire class of developers who don't know what they are doing because of it?
1
u/perestroika12 Sep 23 '16
In some weird way the ease of use for jquery is part of the reason the web is a complete mess. I'd rather have a harder framework that forces you to think about maintainability and design patterns instead of getting running in one line. React is harder to learn but leads to actual long term stability and easy of maintenance.
3
u/Things_I_Said Sep 23 '16
Is there a place where I can learn these design patterns? I'm sort of guilty of that one line jawn
5
u/ClickerMonkey Sep 23 '16
1
Sep 24 '16
[removed] — view removed comment
1
u/ClickerMonkey Sep 24 '16
He doesn't know anything about patterns, so he doesn't really know what he's asking for. Those architectural patterns are more applicable for web development, design patterns are less useful for beginners... they start to abuse them.
2
u/nhavar Sep 23 '16
I think there's a divide here between professional development and lay-development. What jQuery did was allow lay-people to get into JavaScript development pretty easily. At the same time it helped professional developers take some of the pain out of cross-browser development until they had the time to develop more robust custom solutions. You're average hobbyist or tinkerer might not care as much about long term maintenance, scalability, or optimization - they want to play with something, see it work, and get a specific task done. Professional developers care more about the craftsmanship and returns of using a particular library or framework.
With that in mind something like Vue might be good for a certain segment of the development population and grow in popularity that way. That's part of the openness and greatness of the web, even though it also makes it a little messy.
1
1
u/programm_debuggen Sep 23 '16
The React Hello World can be better written to compare to the jQuery version as:
<!DOCTYPE html>
<html>
<head>
<title>React Hello World</title>
<script src="https://fb.me/react-15.0.0.js"></script>
<script src="https://fb.me/react-dom-15.0.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script>
</head>
<body>
<div id="greeting"></div>
<script type="text/babel">
ReactDOM.render(<p>Hello from React</p>, document.getElementById('greeting'));
</script>
</body>
</html>-1
u/programm_debuggen Sep 23 '16
Compares favorably to the jQuery version:
<!DOCTYPE html> <html> <head> <title>jQuery Hello World</title> <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> </head> <body> <div id="message"></div> <script> $("#message").html("Hello from jQuery"); </script> </body> </html>
0
u/spoobo Sep 23 '16
"Vue provides an answer to the issue of JavaScript fatigue". Bold claim. How does it do so?
They claim it is the only tool where you don't need transpiling. And you do for React? No. You don't need webpack for react either. You can just use the plain old way of writing the render function without JSX and it'll work fine. Of course the article forgot to point that out. It also forgot to mention that JSX is a language in itself and not 'another-proprietary-template-syntax'.
Vue seems like a weird mutation between angular 1 and Ember. I doubt it'll be better or more futureproof than Ember and we all know what happened to angular 1.
Actually, if anything solves JS fatigue it's Ember since they have ember-cli which does all the research for you. You just have to type in what you want to happen. I'm not an ember user but I'm just pointing out that the points the author makes for Vue are wrong.
I genuinely don't understand why it is gaining popularity when much better tools already exist that achieve similar results with arguably better ways of doing it. Vue does not do anything revolutionary or even marginally better than the competition. What is the big win for using Vue? Does anyone know? Or is it just because it's gotten the attention of the PHP community?
Also, finding var self = this in the official examples makes me cringe as a JS developer.
3
u/Bloompire Sep 23 '16
I'd rather say that Vue is a "Knockout.JS done right". These frameworks are vastly similar, but Vue seems to address most of the knockout weak spots.
-2
Sep 23 '16
Another example of naive solutions becoming popular because they make a todo app look simple.
-2
-4
121
u/viveleroi Sep 23 '16
I wouldn't ever describe React as the 'new jQuery' to begin with. Even before reading a word of the article I'm questioning the expertise of its author.