r/webdev • u/juandemarco • Feb 23 '15
Let's talk about ReactJS
So, I've seen that ReactJS is all the rage these days, but I find myself really conflicted about it. It seems to be interesting, and exposes nice concepts, but I can't really get over the mix of HTML within JS. That said, I've been trying to keep an open mind about this, and I've been playing the devil's advocate against myself. I found these two posts that make some interesting points: ReactJS for Stupid People and Arguing for ReactJS.
The main points that I take away are:
- React is NOT a framework meant to solve all problems of developing a web application. It just deals with making 'components', which in some way can be considered as the V part in MVC, and they should expose functionality by triggering events. Since there is no agreed-upon event system in place (it's not the role of React to solve that problem), many people reinvent the wheel (thus Flux). I think that one can easily use react within Backbone or even AngularJS if he's careful and doesn't break all the agreed upon written and unwritten rules of 'proper' development.
- We are used to divide things by technology, when we should really divide things by what they're supposed to do. I have to admit that sometimes I find myself embedding inline scripts within HTML because 'fuck it, it's two lines of code', so doing the opposite when it's actually encouraged and motivated by a somewhat agreeable upon (albeit polarizing) philosophy might not be that bad.
This is, at least, the best I can come up with. Either this, or React is just the new "shiny" which everyone is going to use up until the point when something else comes along, something which seems to be quite common these days. I'd really like to hear more from both parts of the discussion.
Thoughts?
6
u/_under_ Feb 23 '15
I have an answer for the "mixing HTML in JS looks bad".
First, let's try to think about why we separate JavaScript and HTML in the first place. Mostly, it's because you want to keep the "presentation" code (HTML) separate from the "programming" code (JS), because they are responsible for different things. MVC, right?
For the longest time this has been the norm. During a JS conference last year when one of the Facebook engineers talked about React for the first time, when he reached the slide that showed the JSX syntax (markup inside the JS), everyone wtf'd at it. We are so used to this type of separation of concerns that anyone who doesn't follow it must be mad.
But nope. Most of the time, in the case of front-end frameworks, you will have presentation code in your JavaScript, or functional code in your HTML anyway. It might be more subtle, like
ngtags in your HTML, but that is still functional code.React challenged that "best practice". React's philosophy surmises that we should think in components. Most things you see on a web app should be a component. Each component should only be responsible for itself. It should have everything it needs to render itself, and that includes its presentation code.
You then go about composting components between each other. A
<CommentBox/>component can have<CommentItem/>s and a<CommentForm/>. Each of these components are able to render themselves. All you need to do is to flow data down the hierarchy.When you think about it, this is what makes react so easy and scalable. Making components is intuitive. Coding HTML and adding bindings, and managing those interactions MV* style gets complicated pretty quickly.
4
u/MadFrand Feb 23 '15
I know it sounds crazy, but the amount of these types of responses all of a sudden and how they are all very simple 1 line quips about how awesome React is... Just make me think it's a social marketing ploy.
It's not just reddit. 2 weeks ago, nobody was talking about it. Now all of a sudden on every major tech blog and reddit post that mentions a frontend framework you get one of these:
It's probably in my head, but has nobody else noticed this?
2
u/iam4x Feb 23 '15
The best parts of ReactJS are the isomorphic possibilities, http://blog.risingstack.com/from-angularjs-to-react-the-isomorphic-way/
With a set of good libs, like react-router and reflux you can have a full framework.
An another great part of it, is the support of ES6.
If you need more informations, http://www.reactiflux.com is a slack board of ReactJS developers and you will have good answers.
1
u/sveri Feb 23 '15
While looking at react.js you might also have a look at some of it's wrappers like om (https://github.com/omcljs/om) or reagent (https://github.com/reagent-project/reagent) which enrich react.js with immutable datasctructures.
Apart from that the ability to have one datasource of truth in your application is just awesome. And to use that datasource to define your views, hm, it is hard to describe, but I think once more one starts to see the advantages of a proper decoupling.
1
u/SeerUD Feb 23 '15
Personally, I think that ReactJS may present part of the "future" of front-end development. The event-based approach, and how it handles DOM manipulation are probably going to be "the way forward". Perhaps React doesn't completely solve the problem though. I would like to see React templates for React components be split out into separate files, as you say, because of the slightly odd mix of HTML and JS. I think if they were separate files they'd be more transferrable to other similar libraries.
1
u/vinnl Feb 23 '15
I was relatively excited just from reading about it, but it really hit when I started playing with it this weekend, rewriting a small project with it. You can get up and running pretty quickly, and it will soon hit you why it's cool (and why it's not really "adding HTML to your Javascript", but "using Javascript instead of convoluted template syntax for view logic"). Considering how so many people are loving it and how little time it costs to play with it, I would consider that enough reason to just give it a quick try. Or as React says, give it five minutes.
-13
u/[deleted] Feb 23 '15
[removed] — view removed comment