352 points · by tilt · 11y ago · 142 comments
That's a best practice for web sites. If you're making a website (where every page has roughly the same look and structure, but just different content), then it's probably a good best practice to have.
It all went haywire, however, when using the web for applications. While well-designed applications also have commonalities to them (button styles, margins, etc), the various screens and dialogs in an application vary way more in structure than the various pages on a typical informational web site tend to do.
It turns out that separating logic and presentation in web applications the way you would on web sites is a very bad match. You simply end up putting logic that is very closely tied together in three different places. E.g. typical jQuery code that falls apart the moment you change either the HTML (turn a div into a span, whatever), or the css class names, or the jquery selector/handler code, all of which is located in different files deep in a different directory hierarchy. For each single widget in your app (button, dropdown, LoginScreen, etc), it's much better to put all that code together.
And that's what React lets you do.
(note that this means that you really do want to tie your styles closely to your react components too - either inside the JS or with e.g. one .sass file for each React component)
As a web developer for the past 10 years I can't disagree with this more. It wasn't the easiest thing to accomplish maybe 5 years ago but it was always doable and now with web components it's incredibly easy.
React is cool but I wouldn't call separating logic and presentation bad. I would call that a best practice.
Even if that is true, it is not clear that trying to separate HTML, CSS and JS necessarily achieves a useful degree of separation between logic and presentation.
In "traditional" application development, we have long recognised that things like keeping the underlying data isolated from any particular presentation or interaction style can have advantages. However, you wouldn't find a lot of people arguing for creating a dialog box in some visual design tool in your IDE, writing separate code in a programming language to populate its controls and respond to interactions with them, and then somehow trying to join the dialog and the code up while keeping everything at arm's length.
Modular design has always been about keeping related aspects of a program together and minimising other dependencies. This means you can do useful things like changing one part of a program without running into surprising or uncontrolled side effects elsewhere. But if you have different aspects of the application that inherently do change together, as is often the case with HTML/CSS/JS in a web app, there is little benefit to trying to separate them for dogmatic reasons.
No, of course not. React proposes to replace one "best practice" by a different one. In React, the best practice is to divide your application up in a deep hierarchy of components, each of which adds one single little layer of abstraction over the other. Then, put all the logic and presentation that's relevant for that little piece of abstraction together inside that little component.
This way, you have very good separation of concerns - all "Button" stuff is together in the Button component, all AreYouSureDialog stuff is together in the AreYouSureDialog component (which in turn uses Button components), and so on. Because components can only interact with each other via props and refs, you also have very fine-grained control over the interface: it's difficult for the programmer of a parent component to influence the behavior of the child component any further than by the interfaces that the child component explicitly exposes for that (= props, and public methods for use via refs).
How is all that a "get out of jail free card"?
I've found that this design principle works very well with teams, even with junior team members. I've seen people who had never gotten further than building 10000-line CSS files with all kinds of horrible hacks let loose in a React project. After we explained the component principle and made an example component to easily copy&paste, they independently and without further encouragement started making React components for each little possibly-reusable part of the application. Buttons, frames, dialogs, cards, lists, you name it. React really makes it easy to make well-structured code and difficult to make things a mess.
I don't see how and of this impacts any of the responsibilities you mention (good UX, accessibility, etc) so I can't respond to that part of your comment.
> if it’s not curlable, it’s not on the web.
http://tantek.com/2015/069/t1/js-dr-javascript-required-deadOne common way is to have React render into a simulated DOM (e.g. https://github.com/tmpvar/jsdom) and then trigger event handlers with code. You make assertions over the structure of the rendered DOM to know whether things went fine, all without a browser attached.
A dumber way that works remarkably OK too for simpler scenarios is to just have React render stuff to HTML strings and do regexes over those strings. Hacky, and if you're doing serious app development you probably don't want this, but it's a very easy way to get started.
I think about how painful it would be adding a custom button(new behavior) to a notification on a website: you either add an event listener to the dom and listen for it to bubble or add it to the button directly(the easier option). These new web frameworks seem to be adding it directly to the dom node -- we're back to <a onClick="function('value')"> which is faster, but traditionally harder to manage. But since everything is a structured component (all of these things implement an interface) we can build them faster and worry about it on the component level and not the markup level.
I've been playing with React and Riot and it is a very interesting approach. You can build things out a lot faster than you used to and that is what matters in the end. The user will not care if you separate concerns (as long as the page renders and is accessible, but that is another story).
So, how do you reason about that?
For example, we frequently create generic containers that modify the behavior of the child or parent. One example is a container that automatically sets its children to be "sticky" once you scroll past it. Another common parent is one that renders it's children is a modal (lightbox, dialog or similar). Another is a child component that, if you nest it into another, can notify the parent whenever its dimensions change.
It's just two different schools of thought and really comes down to personal preference. Fashion ebbs and flows and what was old is new, and what was new is old.
The core idea is that presentation logic belongs with the presentation; you cannot separate the two. In classical MVC frameworks such as Cocoa, this is accomplished with a controller that is tightly coupled with its view. Most people use Interface Builder and maintain the view in a separate file, but this is purely a pragmatic design allowing IB to use a different file format from your code. You could write the entire UI in Objective-C/Swift code, right in the controller, and in terms of layering there would not be a lick of difference.
React, like other MVC-bases systems, encourage the UI logic to be slim and strictly UI-oriented, and to move the application logic -- that which deals with the flow of data/state -- to a separate layer.
You can _implement_ both representation and logic with React. (instead of JS for logic, and some clunky template language for representation).
This does not mean that you should mix it. usually you end up with "representation" components that are "dumb", and logic components, that do not care about the representation of their children.
see https://medium.com/@dan_abramov/smart-and-dumb-components-7c...
Just because it shares the implementation mechanism, it does not mean that they are mixed together. You can of course, but most of the time, you should not.
In other words, use css classes to style similarly looking elements instead of defining the styles inline (style attribute). Or use event delegation instead of adding a bunch of onClick attributes on your elements.
When talking about developer productivity (ie. maintenance, new feature development) what's more important than separation of technologies is separation of concerns at the "component" level.
Component in this context can be simply defined as a single file that contains all the logic required to render and manipulate a single DOM element throughout its lifecycle.
What React does (as is Angular to a lesser extend) is allow you to mix markup and behaviour while developing but keep those things separate when generating the end result.
React's building block is the 'component' – the idea being that this is an isolated part of the application. By its very nature, isolating a component means separating the HTML from all the other HTML, and the script from all the other script.
Since components are generally quite small, it makes sense to bundle these together. You don't suffer the problems that encouraged people to separate concerns in the first place (things like have loads of interacting code that's duplicated and closely tied to structure), so it's OK to take a different approach – and I must say, in practice it works really well.
CSS is a separate issue – React doesn't mix CSS into the pot at all.
Not quite true, ReactNative does, React supports it[0] and one of the lead devs (vjeux, gave the reactjs conf 2015 keynote) has a presentation on doing exactly that[1] alongside being the lead dev on Facebook's css-layout (which IIRC underlies ReactNative's layouting)
[0] https://facebook.github.io/react/tips/inline-styles.html
[1] https://speakerdeck.com/vjeux/react-css-in-js
[2] https://github.com/facebook/css-layout/graphs/contributors
Sure. React even ships with testing utilities to make that easier: http://facebook.github.io/react/docs/test-utils.html
> I'm wondering how the marrying of presentation and logic affects that.
Depends on the exact component. For pure components as part of bigger applications it's really quite easy, just render the component with a known dataset and see if it's got everything where things should be. Then again, that's similar to testing templates… most people don't really test their templates.
For self-contained "widget" components things are a bit harder, it's similar to testing DOM-based widgets really.
I think a saner approach is to start with plain old data (e.g. some JSON) and have a function that can turn that into html/css/js. This is what react allows by using props/state as the input to the component/rendering function. It also permits you to pass complex objects as props/state but I like keeping to simple data as much as possible.
The CSS always needs enough hooks in the HTML to do it's work, without the right hooks some things are impossible with CSS alone (or would be unnecessarily obscure/tricky/brittle).
HTML isn't a general data format in the way JSON is.
My favourite part is passing functions and complex objects as element properties. Very useful.
The idea is to think of your HTML as a part of your app, rather than just "something that is modified by the JS".
In contrast to Angular where we're told to put logic in templates (or just HTML files) it looks a lot better.
First, they came for the HTML markup and said "come on guys, JSX is just some XML on steroids. You should not worry at all" and we didn't speak up.
Then they came for CSS and said "come on guys, they're just small components and the same as placing the CSS declarations via the style attribute anyway blah blah blah" and we didn't speak up.
So you don't really know what's gonna be their next victim. There's a lot of revisionism and reinterpretation of best practices going on in the industry and hipsters and fanbois seem to go chasing the latest shiny and trendy object out there.
Nothing we can do to help save these people!
But seriously guys, have you seen any React dev code with all those CSS declarations and HTML markup over each other?
I pity the ones who will be assigned maintain this spaghetti code.
https://speakerdeck.com/vjeux/react-css-in-js
I have apps in production with it you can look at the code to as well:
Re productivity, if it works for you, good for you but please don't attempt to reinterpret/bend the rules that are well established in the industry just to avoid criticism. For me, it's just an act of intellectual dishonesty.
React does violate the principle of separation of concerns and "we're working on a component not a document level" is not fooling anyone.
We should alert and educate people on this issue and then they can decide for themselves if they would go ahead nevertheless or stick to their guns. That's all!
BTW - I can see that the section on Flux and Alt is still left to be written. Any idea when you'll have the drafts for those sections ready? Lastly, it would be nice if you could contrast Alt with Reflux and give us your recommendations on what to work with.
Thanks a lot, again!
As for a contrast between Alt/Reflux, I am hoping others using different Flux libraries will contribute and share their views, I will also link to some comparisons others have already done, such as: http://pixelhunter.me/post/110248593059/flux-solutions-compa.... But if I have time to, I'll give it a try!
Collectively, all the parts of the primer, together should give you the tools you need if you want to build a SPA.
With the included PATENTS file, if Facebook decides a patent they filed is something you violate then they will sue you or ask for a royalty. If you try to fight it or even suggest it's not valid then your license for React and all other Facebook projects becomes voided. This gives incredible leverage and power to Facebook for anyone developing their core business with anything they've made/open sourced.
EDIT: https://github.com/Raynos/mercury was suggested as an alternative, I'm not sure how viable it is for different types of projects.
Using code written by Facebook is not a huge business risk, unless you consider the use of any open-source code to be a business risk.
The PATENTS file that Facebook distributes with it's open-source projects grants additional guaranteed rights. It does not place any additional restrictions or risks on your use of the software, above and beyond any that exist with other licenses that do not grant patent rights.
If you "try to fight it or even suggest it's not valid" then you do not lose your license for react and all other Facebook projects – what you lose is the additional patent rights grant. That's a huge difference!
"Facebook is granting you additional rights on top of the BSD license. In the worst case, where you do something that causes the patent rights to be revoked, you're still left with the same rights you have under the BSD license. This is unambiguously safer to use than if they had released it under a vanilla BSD license. As for why Facebook did this, they're most likely trying to give away additional rights while still maintaining the ability to use these patents defensively, in the event someone sues them for infringing a different patent."
This is 100% totally and completely wrong :)
The BSD license normally includes an implied patent license.
It says " Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met .. "
This grants an implied patent license to the patents necessary to actually do this, as long as you meet the restrictions.
This is actually well settled law. You don't get to give people stuff unrestricted, and say "you can use this for free", and then say "just kidding, what i meant was, you can use this for free as long as you pay me for the patents" However, when you do what facebook has done, and give an explicit license, you have overwritten the terms of that implied license, and you get no implied license.
So it is not only "not unambiguously safer", you are not "left with the same rights you had under the bsd license if it is revoked".
This is because if you revoke the explicit grant, you get nothing in terms of patents. But the implicit grant is not revokable unless you violate the copyright license.
So sorry, but this is not "better than the default" and does not in fact, help you.
Besides, Facebook is gonna sue you if you violate their patents, regardless of whether you use React or not. And the termination pertains to the patent license. They can't restrict your access to a BSD licensed software package.
TL;DR: Facebook is changing the BSD license from being an implied license to being an explicit license, so it's not the same as BSD license.
* If Facebook is "changing" the BSD license, why is the BSD license included verbatim and in full?
* Also, why does the PATENTS file explicitly state that it is an "ADDITIONAL grant of patent rights"?
* Why don't I see comments like the one I originally replied to being posted on articles pertaining to Apache? They have almost exactly the same wording in their statement.
I'll believe claims of Facebook "changing the BSD license" when they actually change the BSD license.
Also is this a good direction for "mobile first" approach? What about platform independence? (iOS/Android/Desktop agnostic, etc, etc). (bootstrap? phonegap?)
In comparison with Backbone/JQuery, React is more declarative. It encourages the developer to describe the structure of the UI in a given state and let the engine handle the transitions between states. It's possible to use Backbone models and events with React views with very little impedance mismatch.
I would like to hear an Ember expert's perspective.
React Native has an interesting approach to mobile-first development. The general philosophy is "learn once, write anywhere" instead of "write once, run anywhere". Most apps cannot make the most of their platform while being truly platform-independent, but unifying component lifecycle behavior between platforms could end up being easier on developers than learning the particulars of each platform, while producing a better result than a PhoneGap-style wrapper.
I just wish we had a non-proliferation treaty on Flux frameworks.
For example, I like Fluxible because it lets you do server side rendering, and it avoids singletons, so it's exceptionally easy to test.
However, if I'm working on an app that doesn't have any public facing components, I'd probably consider a different implementation that is less verbose.
https://github.com/spoike/refluxjs
[Edit added link to Reflux]
Of course, it took way longer to put together a good stack. I spent months pushing React and trying to learn the best practices there.
I did end up with something relevant though. I built Reapp: http://reapp.io
It's really a bunch of things in one, but I think it's hugely overlooked for being an awesome bootstrapping method for getting a full-featured React app up and running. You can have your app running with hot reloads, ES6, cordova-builds, flux, etc all in minutes with a single command. And of course it includes an awesome (IMO) UI kit to boot.
But basically the answer is: yes, you can! You can even write an SPA using nothing but the regular W3C APIs! React just makes it very easy to keep the DOM in sync with your application state.
[0] - https://github.com/mikechau/react-primer-draft#react-router
1: https://facebook.github.io/react/docs/events.html
2: https://facebook.github.io/react/docs/component-specs.html
It includes react-router, and quite a few good libraries/examples for flux, requests, immutable.js, etc.
Sure. You may not want to depending how big and complex the application is, but it's definitely possible.
A router and an immutable collections library (immutablejs or mori for instance) are very convenient additions, but by no means necessary. Hell you don't need a precompilation phase either by skipping JSX (and unsupported ES6 constructs).
Checkout: http://youmightnotneedjquery.com/#ajax
Get over it nerds. React is very well thought out, and Just Works™. Your me-too diatribe about how being different is bad doesn't help anything but feed these endless debates. Build apps or die. If you prefer building lousy implicit Rails apps, go wild.
There's an angular review [1] that I believe hits the nail on the head:
"Angular wants you to build your application in a particular way, but it’s not very explicit about it. Call it “passive-aggressive architecture.”"
After using angular for over ~2 years, I'm far happier with react (and a flux implementation, if doing something more complicated). You end up with far simpler code and more maintainable code.
The fact that the angular team itself is doing a complete rewrite that throws away almost all the fundamentals of angular 1.x shows a clear recognition of the many flaws present in it.
[0] https://github.com/johnpapa/angular-styleguide [1] http://www.letscodejavascript.com/v3/blog/2015/01/angular_re...
Consider how difficult it is to open different sections of your site into separate tabs.