News for hackers. Stuff that matters.
Re:React Hot Loader: Tweak React Components in Real Time by jasim on Monday February 02, 2015 @12:02AM (#8982798)
This is amazing stuff, thanks Dan! Hopefully it is the beginning of the end of refresh-driven-development.

If you are a web developer (front-end/back-end), and you haven't switched to React yet, take this for a spin, and read "Facebook just taught us all how to build websites" (https://medium.com/@ericflo/facebook-just-taught-us-all-how-...), and watch 'React - Rethinking Best Practices' by Pete Hunt (https://www.youtube.com/watch?v=DgVS-zXgMTk).

The distinction between the back-end and front-end is a product of the complexity introduced by the web with its 'worse is better' mix of badly integrated tools. It made building user interfaces a dark art with all its quirks:

- a layouting system that had an active vendetta against humans

- Javascript with all its deficiencies

- Connascence introduced by Server <-> Client communication (Server Model <-> API View <-> JS Model <-> JS View)

- declarative views that were ill-suited for web-apps (logic-less templates, what a lie!)

- cross-browser incompatibilities

- difficulty in implementing client-side routing

- immature developer tooling

But worst of them all was DOM mutation and spaghetti code, which was in reality a fundamental deficiency of the development model itself, and not because web developers the world over were being deliberately irresponsible.

React is changing all that with the help of ES6. It simplifies web development both in concept and in practice. In a few years, building UIs would be just another part of writing software, and competent Full-Stack developers would cease to remain unicorns.

Here are a few things that React approaches differently that makes all the difference:

- templates are a bad idea. views are constructed imperatively, not declared. (plug: http://www.jasimabasheer.com/posts/on-react.html)

- the basic unit of a front-end system is a 'component'. A component knows how it is rendered (HTML), how it is styled (CSS), how it behaves (JS), and has explicit knowledge of the data it handles (states and props).

- everything should be programmable: even HTML and CSS. (https://speakerdeck.com/vjeux/react-css-in-js)

The React team seems to be looking at what makes web development actually hard and are thinking outside ill-advised conventional wisdom (like separation of concerns. whose concerns anyway?). For example, Relay (https://gist.github.com/wincent/598fa75e22bdfa44cf47?), removes the need for writing custom API end-points for each of our components. A component already knows the data it needs, but today we leak that info across the component hierarchy and to the server, creating a lot of accidental complexity.

Another example is Webpack (http://webpack.github.io/), which changes asset management on the web by making it completely programmable. All your static assets should have the same bundling conveniences that your JS has. Webpack also makes react-hot-loader possible with Hot Module Replacement.

React seems to have finally figured out the right direction to take the web to. Exciting times.

Re:React Hot Loader: Tweak React Components in Real Time by drdiablo on Monday February 02, 2015 @01:40AM (#8983000)
I think you forgot something that is very important to React: your component is a pure function that takes props and states and that outputs UI. Being able to make the assumption that given the same props and state, you'll get the exact same UI (to the cursor position), is extremely powerful. As a programmer your brain can focus on other things and forget about side nasty effects.
Re:React Hot Loader: Tweak React Components in Real Time by Touche on Monday February 02, 2015 @09:37AM (#8984292)
In React your component is a class. The `render` function is a pure function, but the component itself is a state machine. This is why I prefer Mercury[1]

[1]https://github.com/Raynos/mercury

Re:React Hot Loader: Tweak React Components in Real Time by danabramov on Monday February 02, 2015 @11:50AM (#8985119)
Also Cycle: https://github.com/staltz/cycle/

I don't prefer them over React, but I agree they make this stuff easier. React Hot Loader is a hack after all ;-)

Re:React Hot Loader: Tweak React Components in Real Time by jasim on Monday February 02, 2015 @06:29AM (#8983681)
True. No more spaghetti ball-of-mud. I'm waiting for IntelliJ to release `Extract custom render method` and `Extract React Component` refactors in their IDEs.
Re:React Hot Loader: Tweak React Components in Real Time by danabramov on Monday February 02, 2015 @07:05AM (#8983776)
Hell yeah. That would compel me to switch to an IDE. In fact a React component-centric IDE would have been awesome.
Re:React Hot Loader: Tweak React Components in Real Time by zaroth on Monday February 02, 2015 @12:15AM (#8982822)
A post so nice, would upvote twice! Thank you for the summary and bevy of helpful links!
Re:React Hot Loader: Tweak React Components in Real Time by Kiro on Monday February 02, 2015 @03:22AM (#8983214)
> A component knows how it is rendered (HTML), how it is styled (CSS), how it behaves (JS), and has explicit knowledge of the data it handles (states and props).

I don't get this. Sure, components seem sound but often a component is dependent on its context. So a component in the header may need different margins for example than one in the footer. How is this handled in React where the components seem really isolated?

Re:React Hot Loader: Tweak React Components in Real Time by jasim on Monday February 02, 2015 @03:47AM (#8983276)
This question has more to do with how we think about abstractions than React itself.

In plain CSS, you can specify a specialized parent -> child rule that affects the component just in this context. Or you can have two different classes for the two cases. You can also define two distinct container elements that compose the same underlying component, but the container takes care of the margin depending on the context.

All of these options work with React as well. However, it lets you go further: you can pass the context info into the component, which then adjusts itself. Or the parent element in both cases can wrap the component inside appropriate html elements, or have specific container components, all using imperative Javascript to construct the view.

With React you have the entire repertoire of programming abstractions to solve this problem with, while traditionally we had to work within the confines of CSS's cascade.

But in cases where internal layout of the component is very different, we have to make a call between adding conditionals within a component vs giving distinct names to the different cases and extracting multiple components. I veer towards having more explicit names in my codebase and so extracts new components more often than I litter existing ones with conditionals.

Also, in practice, components are surprisingly self-contained and independent of context.

Re:React Hot Loader: Tweak React Components in Real Time by antris on Monday February 02, 2015 @03:55AM (#8983299)
The same exact problem exists inherently in CSS itself and has been addressed in many ways through different kinds of conventions.

One way is to mix two different sets of styles for the element.

Set 1 gives the element the general styles of the component itself.

Set 2 gives the context-specific styles that are only used in that one instance.

It's just like using two different classes. E.g. `.button` and `.firstButtonInHeader`. And then having an element like <button class="button firstButtonInHeader">.

Since components in React are declarative, instead of using a restricted tool such as CSS classes, you have the full power of JavaScript to construct these mixins however you like.

Re:React Hot Loader: Tweak React Components in Real Time by Bahamut on Monday February 02, 2015 @01:14AM (#8982939)
A few of these points are a bit off.

React isn't against templates - the existence of JSX is counter to this. React does choose to consume templates in an imperative form (via React.DOM), and JSX is meant to ease this flaw in productivity by giving tooling that corrects this unnatural form of development (for most frontend engineers).

React is also not against separation of concerns - even in the talk by Pete Hunt linked, he merely has a different view on what constitutes the concern of the view layer. His viewpoint is that the template and the state of the component are intertwined heavily, so as to stress that Facebook's approach with React does not break that principle.

Re:React Hot Loader: Tweak React Components in Real Time by jasim on Monday February 02, 2015 @01:38AM (#8982994)
Thanks for your thoughts. I should clarify what I meant by templates.

Templates are driven by content, not by logic, and they are typically monolithic. This works well for web pages which are about content which doesn't have much JS. The web was historically content-driven, which has started changing only recently with the evolution of Javascript and the emergence of web-apps. This historical quirk is why imperative construction of the view is unnatural form for developers. This is however going to change fast, because imperative view construction is the natural way to build web apps.

JSX templates are templates in the sense they do minimal string interpolation. Everything else is taken care of by programming language abstractions. They include control structures: conditionals and loops, and primitives for abstraction: variables, functions and objects. A template 'programming' language on the other hand is a suboptimal mix. For example, Angular's templating language reminds me of Greenspun's 10th rule, which with suitable modifications, can be read like this:

  Any sufficiently complicated templating language contains an ad hoc,
  informally-specified, bug-ridden, slow implementation of a 
  turing complete programming language.
I cover the above perspective in detail here: http://www.jasimabasheer.com/posts/evolution-of-view-templat...
Re:React Hot Loader: Tweak React Components in Real Time by modarts on Monday February 02, 2015 @03:33AM (#8983243)
This comment is incredible. Please expand it into a blog post.
Re:React Hot Loader: Tweak React Components in Real Time by nightski on Monday February 02, 2015 @01:05AM (#8982919)
I like a lot of what I see with react except the lack of 2 way data binding. Mainly, I tend to have input data tables for forecasting, cost estimation, and so forth with a lot of data inter-dependencies and calculations. It seems going back to the method of handling data updates through onchange events in a manual fashion would be a colossal step backwards from say, knockoutjs.
Re:React Hot Loader: Tweak React Components in Real Time by danabramov on Monday February 02, 2015 @06:07AM (#8983634)
Nope. Lack of two-way binding is a blessing. It makes everything predictable.

>It seems going back to the method of handling data updates through onchange events in a manual fashion would be a colossal step backwards from say, knockoutjs.

It's not exactly "manual fashion" because React updates everything according to `state` for you. If you have many interdependent fields, make them update `state` correctly, and React will keep them in sync with it.

In fact I find it easier to make complex input logic with one-way bindings because the data always flows one way. It's a tad more code to write, but you see exactly where your updates are happening and what's causing them.

Two-way binding tends to get very complicated as soon as you need more complex logic. One-way binding doesn't have this problem.

Re:React Hot Loader: Tweak React Components in Real Time by nightski on Monday February 02, 2015 @01:49PM (#8985931)
I'm not sold two-way binding tools get inherently complicated, I just haven't seen this increase in complexity in our current app which is quite large. But I am attracted to react for the reasons you mention, the virtual DOM aspect is very nice so I'll give this a shot.

Although even though ReactLink works it a.) feels like quite the hack (i.e. not very composable) and b.) is a more code to maintain still when it comes down to it.

Re:React Hot Loader: Tweak React Components in Real Time by dlb_ on Monday February 02, 2015 @01:21AM (#8982953)
You can do two-way binding of a form field to the component's state using the ReactLink helper:

http://facebook.github.io/react/docs/two-way-binding-helpers...

Re:React Hot Loader: Tweak React Components in Real Time by j_s on Monday February 02, 2015 @11:05AM (#8984812)
Take a look at HN's discussion on Facebook's Data-fetching framework for React (Relay), due for release 'real soon now':

https://news.ycombinator.com/item?id=8977229

Re:React Hot Loader: Tweak React Components in Real Time by nightski on Monday February 02, 2015 @02:03PM (#8986034)
If something can't be described very simply, it scares me away. There are a lot of decent individual ideas and concepts in this gist but from an overall perspective it is very concept-stage stuff which leads me to believe it is a lot farther from a well honed proposal then we are being lead to believe.

If they deliver something concrete and simple it could be great. But there are way too many questions at this point to even start to get excited.