r/javascript • • Jun 28 '18

The exact same app - 1 version made with React, Redux, and React-Router The other made with Vue, Vuex, Vue-Router. Good if you want to see both worlds. User Auth with JWT, note CRUD actions, Koa (Node) backend. MIT license.

https://johndatserakis.github.io/koa-react-notes-web/
459 Upvotes

98 comments sorted by

View all comments

Show parent comments

54

u/adstwlearn Jun 28 '18 edited Jun 28 '18

Great question. For me, it's Vue.

All in all - I did like React. Not too hard to pick up. It's complete, and has a great community. The thing is, it seems like there's less batteries installed. That can be a plus at first, but after you sorta get the idea of how things work, it seems like you're just writing more code then needed.

Like with React-Router and how, if a component was not a direct descendant of a Switch statement main route definition, then it didn't have access to the history prop - and I had to wrap it with the withRouter HOC. That was just a bit funny - although I do like how some components are truly dumb vs smart - I guess that's the intent. It's just a bit different from having the $router always available to use through this.$router in Vue. Small things like that stood out.

The strangest thing for me in React was the render function. In Vue it's just so easy to loop over data and spit out elements or show/hide data based on state/store variables - in React it felt pretty strange to have to create my notes loop outside of the render.

In Vue, if you want to show or hide something - just use <div v-if="myVariable">Lorem...</div> and it'll be based of your myVariable truthiness. In React it seems like you have to do:

{this.state.myVariable &&
  <div>Lorem...</div>
}

It's a little more verbose, and doesn't support that instant-helper looping that Vue can manage. But of course, after I familiarized myself with how to do those normal little things, it didn't seem too odd. It's like, ok, whatever - that's just how you do that.

One other thing - the redux and react dev tools are definitely not as nice as the Vue tools - from the style, to having to open a huge tree of elements just to see a component's state - it made it a bit tough to get a view of my app's variables. I may be missing something here though or using a version that isn't the current community standard.

I definitely like React, and I do feel it was important to spend a solid month with it to learn its intricacies, but I do think I'll be using my Vue setup going forward for most things. It's less code and a simpler coding experience with helpers that make sense and are just magical enough that they're helpful, but not too magical that I lose track of what's actually going on.

16

u/Capaj Jun 28 '18

Like with React-Router

I dislike this about react-router too. Way too verbose. Version 4 is better than previous ones, but still too lame. @reach/router now that's a router I love! It makes routing in react easy and fun. Too bad it doesn't support hash history.

0

u/[deleted] Jun 28 '18

[deleted]

14

u/spooky___ghost Jun 28 '18

An important point you're missing is that any component that is receiving route information as props will be re-rendered anytime the route changes. On top of the other points mentioned above, opting in to receiving routing props is 100% the correct decision.

In real applications only very few top-level components require any route state. If child components require it they can opt in or you can pass it down through the prop chain. React is all about giving control to the developer you do not want anything to be automatic or implicit.

10

u/[deleted] Jun 28 '18

I think this is exactly the main difference between React and VueJS. Vue requires you to be comfortable with magic and tbf that magic makes 90% of use cases really easy to spin up and develop.

React on the other hand has a small surface area.
It really is basically (state, props) => view.

It's essentially functions all the way down. With React you know exactly when and why a component will re-render at the cost of maybe having to write more code (personally I think it's a small price to pay).

If you're building a few pages and forms you should probably go with Vue, you'll love it and it'll really speed up your development.

If you're building a complex desktop grade application React gives you more power to realize that and the explicitness is a godsend for maintainability.

7

u/[deleted] Jun 28 '18

Why not? I don't want routing available in components that don't need to know about routing.

This lets me be explicit about what I pass in. Why should my say Text component need to know what the current route is or have the ability to change it?

→ More replies (5)
→ More replies (1)

12

u/[deleted] Jun 28 '18 edited Jun 29 '18

[removed] — view removed comment

8

u/sonofamonster Jun 28 '18

It has the best name though. It sounds like it’s the router that you’re supposed to use.

6

u/equake Jun 28 '18

Are there good alternatives?

2

u/[deleted] Jun 28 '18

What about it?

5

u/TheMr_ Jun 28 '18 edited Jun 28 '18

In regard to the return function, I see it as more separation of concerns. JSX is already being intermingled within each component alongside the normal JavaScript. For me, I'd say it helps keep the UI and Data separated more. What I found when learning React was that it tries to rely heavily on vanilla js where it can. Learning React taught me a lot more what could be done with vanilla js and has improved my overall understanding of just javascript as a whole.

And with that, it's fairly easy to loop over data within React just with plain javascript. Simply map over an array and pass in a component or element to be returned.

let state = {
    array: [1,2,3,4,5]
}

render(){
    return(
         <React.fragment>
             {this.state.array.map(number=><span>{number}</span>)}
         </React.fragment>
)}

Which will be rendered as the following into its parent container

<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>

You can also use the filter and map functions in conjunction to check whether an item should be rendered at all.

3

u/ImOutOfThisWorld Jun 28 '18

you forgot the "{}" in:

<React.fragment>
    {this.state.array.map(number=><span>{number}</span>)}
</React.fragment>

2

u/TheMr_ Jun 28 '18

Can't believe I forgot that. Thanks, I've edited my reply

3

u/crazyfreak316 Jun 28 '18

Learning React taught me a lot more what could be done with vanilla js and has improved my overall understanding of just javascript as a whole.

I can completely relate.

2

u/ihsw Jun 28 '18

<React.fragment> elements can be replaced with empty-bracket elements.

Eg:

<>
    {this.state.array.map(number=><span>{number}</span>)}
</>

And, furthermore, you can return bare arrays of rendered elements, so even that is unnecessary.

render() {
    return this.state.array.map(number=><span>{number}</span>);
}
→ More replies (1)
→ More replies (2)

6

u/imatworkyo Jun 28 '18

I appreciate this run down and overall agree. But its interesting you seem to still be leaning towards React

- to me, we share the same opinions; although I am a tad harsher on React ( there are just so many special idioms and complexity compounds quickly on larger projects ). I just find it interesting that you seem to land on a different ultimate conclusion.

To me Vue is the way forward, its simplicity and ease of cognitive overhead is refreshing while supplying most of, if not all of the benefits of React

4

u/Charuru Jun 29 '18

Did you read it wrong? His preference was for vue.

3

u/imatworkyo Jun 29 '18

probably did

→ More replies (2)

4

u/[deleted] Jun 28 '18

What are the special react idioms?

To me React has less "React" stuff than Vue does "Vue" stuff.

5

u/jsyoda Jun 28 '18

Vue has more special idioms than React imo - Vue has its own dsl in html - can you explain what you mean here?

I think where React shines compared to Vue is when an app becomes more complex:

When a piece of state updates in Vue, you set its new value. Is another piece of state being updated here at the same time by listening to this value? You don't immediately know and need to look around - tracking the state changes can be difficult.

When a piece of state updates in React, any other state changes that occur at the same time are immediately obvious.

3

u/adstwlearn Jun 28 '18 edited Jun 28 '18

Seems like we agree - maybe I wasn't clear in my comment. I will be using Vue going forward for most things! (Will probably do a few smaller things in React to stay sharp at this point)

2

u/jkoudys Jun 29 '18

Does "less code" really imply a "simpler coding experience", though? As a 3rd-party reading your code, I look at something simple like your App.vue, and look at how many modules are implied to have loaded without actually specifying its source. The <footer-main> for example, requires that your router.js is loaded, which registers a Vue.component (essentially a global) so footer-main is your Footer.vue. Not only is it harder to reason about, but it limits us if we want to code-split the chunk too.

If you're using direct imports, every missing one shows up as a big squiggly red line from static-analysis in my editor long before runtime. Every non-trivially small component can be included via an import(), so it only gets its chunk loaded when I decide it's needed.

There's a lot more that forgoing pure ecma for a micro language hinders you for too. Dead-code elimination is basically impossible, both because Vue.component and router are essentially globals, and because so much of the control logic (e.g. v-if) is in microlanguage. There's the complexity of needing to learn, write, and reason in multiple languages at once (both harder to build and reason about -- with React and emotion, I don't need a single line of CSS, or to write any HTML beyond the tiny boilerplate react-create-app makes).

Don't think I'm being too critical - super happy you wrote this and released it to everyone. I do think a lot of your problems with react are based more in familiarity with vue, and React not being enough like vue isn't really a flaw. Some of your criticisms of individual libs (especially router) are pretty spot on, but you're also not following some basic patterns that React devs (especially the FP-loving ones) routinely use. e.g. out of array methods map, reduce, filter, find, Array.from, [... ], etc., I find only a single Array#map, which is pretty unusual for a React app. React's all about passing in the same collection as a prop to different components, and rendering them differently, e.g. a collection of restaurants could be mapped to a bunch of pins on a <GoogleMap> in one place, a text list in your <Itinerary> somewhere else, Vegan options: {restaurants.filter(({ cuisine }) => cuisine === vegan).length} in another component, etc.

2

u/adstwlearn Jun 29 '18

Upvoted. I don't think you're being too critical. It's just one of those things where this is a full example, so there's a lot of moving parts. I would argue that things like <footer-main> all follow the same exact pattern, so while it may seem strange at first, you'll pick up the structure right away.

Regardless - there's definitely something to be said about how you have to explicitly do certain things in React - like adding routing support to the footer if you wanted to push from there. I can definitely see the argument that it keeps things more focused and compartmentalized.

→ More replies (1)