287 points · by friggeri · 13y ago · 112 comments
I'm really happy to see Web Components (inc. Polymer, Angular, x-tags, and React) landing into HTML. If we accept that web apps != web pages at a spec level, hopefully we can build an awesome platform on which to create tools that leverage all the great parts of the web (linkable, available, auto-updating, device-agnostic) without the mess that we've made trying to make the web do something it wasn't designed for.
I really wish we took it back to a document approach. True REST, everywhere! That's new and novel. Frameworks that build in behaviors with static html - that's just a poor mans gui toolkit. I suppose the web will have go through the task of figuring out its just another in a long line of mistakes. It's like people want to rebuild xslt but badly.
I've also used Ext and FB React certainly reminds me of it. The biggest problem with ExtJS is that it wanted to be my class system, my service layer, my UI, my CSS, my everything. The APIs were convoluted. Then they switched to MVC when that was cool, but it was kind of a kludge.
Surprisingly, Angular seems to get functional modularity. Directives and filters are easily sharable, very good examples of modular components that can just be simple functions. The other part I really like in Angular is that a model is just data. Got an array? Sure, that can be a model.
I keep waiting for the point where I feel like those dirty Angular devs tricked me into using their framework, but it hasn't happened.
It was never intended for anything else and the fact that one has to navigate between html documents proves it.
Try to create a "webapp" without an html page -> doesnt work.
the whole web model is about documents and links.
THAT SAID
it doesnt mean one cant do awesome stuffs with it. But sometimes developpers forget the basic of HTML when developping javascript heavy products. I myself am working on a big modeling app for CS students with angularJS ( MV* ) and jquery (+ a ton of plugin). AngularJS makes it very easy and clean.
<?php
// note: includes omitted
if ($_POST['name']) {
echo <span>Hello, {$_POST['name']}</span>;
} else {
echo
<form method="post">
What is your name?<br />
<input type="text" name="name" />
<input type="submit" />
</form>;
}
in that snippet, the $_POST variable is correctly escaped to mitigate XSS attacks. XHP also allows you to condense complex components into a single tag.React is designed for building big UIs. The type where you have lots of reusable components that are handling events and presenting and changing some backend data.
In a traditional MVC app, React fulfills the role of the view (and maybe the controller, depending on your definition).
React's technique is to break your view down into smaller, composable components. These components provide a render() method which specifies how the component will generate its markup. render() can either return normal DOM elements (like <div>s) or can return other components.
When your data changes, the render() method is called again. We compare the old render()'d version with the new one and the framework determines the most efficient way to update the DOM. Often this will be faster than hand-coding it, as React knows about the entire state of the page and can do cool tricks like batching reads and writes.
The way we're able to pull this off is by constructing a very fast, lightweight representation of the DOM which knows which parts are dirtied and which parts are clean.
Because this rerender is so fast (on the order of 1ms for TodoMVC), we don't need the end user to explicitly specify data bindings. We've found that this is an easy way to build apps.
We have many examples of using React with Backbone as the backend, or using it in conjunction with something like Bootstrap's JS widgets. They're available at https://github.com/facebook/react/tree/master/examples/.
Does this answer your question? As we've just launched I could definitely use some feedback on how we could explain it better!
1) One of the great thing of React to me is the fact that we're writing everything in Javascript. With a very small syntax enhancer that is JSX you can write XML inside of the Javascript.
The end result is that you don't need to learn a template language or many new concepts such as all the ng-repeat, or code inside of strings that angular has.
If the framework doesn't support something, you can just implement it yourself. The React dom nodes are just regular javascript objects and you use regular arrays to manipulate them.
It is also very convenient to be able to write XML in Javascript. You work with html trees all the time yet you can't easily make new ones in js.
2) Having a representation of the DOM in javascript is a huge enabler.
Out of the box we can batch all the expensive DOM operations and only update the parts that actually changed.
But it allows to do more crazy things. We can put all our application logic in a web worker or render the page server side. There are a lot of cool possibilities once you are in a React world.
3) One of the huge issue we face as js developer is traversing the dom to get a reference to whatever node we want. jQuery shine with that. React transforms the paradigm. Instead of finding dom nodes you are creating them.
It leads to different ways of seeing the world. If you want to hide an element, instead of finding it and hiding it, you just don't create it in the first place.
Other view model frameworks typically track all changes to the view model by explicitly firing an event for every delta change. That in turn causes another delta and so on. This is similar to SVN where every change is a delta from the previous state. That means that you'll need to codify every possible diff through your view hierarchy/graph. For simple stuff, they have built-in tools to help you do this. For complex views, that's really difficult or impossible (you can get into conflicts and deadlocks).
React makes it easy by simply regenerating a copy of the next view. This is similar to Git where every change is a snapshot.
There are benefits with either architecture, but I'd recommend you figure out what benefits matter more for your use case. I'm just pointing out one area where React is different.
var HelloMessage = React.createClass({
render: function() {
return <div>{'Hello ' + this.props.name}</div>;
}
});
Wait, what's going on here, how come the return string isn't wrapped in quotes?EDIT: Oh I see it's something called "JSX." Interesting.
npm install -g react-tools
and you'll have a jsx command available. We cover this towards the bottom of this page: http://facebook.github.io/react/docs/getting-started.html
edit: The fact that the pre-processing library is 300kb uncompressed is a pretty huge deterrent.
The next step is to create a compiler for your `anything-you-want` language. That means it transforms the textual contents of the script tag into a string of javascript, and then calls `eval` on the string.
* This breaks code editors, static analysis tools like jshint etc.
* I think Web Components is landing soon, and this seems like it could leverage that.
You're right. One of our goals in the coming months is to try to make some language files for editors. Vim actually does a pretty good job of this already thanks to the similarity to E4X.
JSHint is another thing we'd like to improve upon. At Facebook & Instagram, we actually lint against the transformed code. Right now the transforms don't modify line numbers so it's been easy to match up. We'd like to build some tooling around this for people who make use of React and JSX.
> I think Web Components is landing soon, and this seems like it could leverage that.
I like the way you think! We've started thinking about that already.
When you need this, you need it. When you don't, prefer static html.
Keep in mind, the folks who write developer documentation for things like this have had their head in this problem space for a long time. So there's less explanation of the "what" and "why", and examples tend to be more oriented to the "how".
He didn't said "static html". He said "straight html". Which I presume means "also factoring in plain JS code".
React gives you a set of tools to generate markup safely. The syntax we prefer to use for it is JSX, but if you'd prefer to do it your own way, that's great too. You can organize your app however you want by splitting the DOM generation code into separate files if you wish.
https://hellojs.backliftapp.com/search/React
This includes the compiled version, which isn't included by default.
https://github.com/facebook/react/tree/master/examples/
React is not a standard MVC framework -- we simply strive to be a fast and easy-to-use way to build UI components. You can integrate with any backend (or frontend!) stack that you want.
The reason you'd probably want to use React is that there aren't a lot of concepts to learn, it's fast out of the box, and it ensures that your UI stays consistent with your data without manually specifying data bindings or anything like that.
P.S. It doesn't work now with react-tools installed from npm due to bug with their release, which is now fixed in the repo but isn't released yet.
I wonder though if Apple would ever allow their FB iOS app to run FB apps, as iOS guidelines forbid apps from downloading "code in any way or form".
However it's definitely possible to build a component library with React, we just haven't done it yet.
If anyone wants to build bootstrap or jQueryUI bindings, we're taking pull requests :)
I was encouraged by the sentence "... This is what makes React composable: a key tenet of maintainable frontends."
In my experience, using concepts from functional programming is a good sign for the long term health of a program.
Here's how I lay out my SPA structure :
http://anexiledderryman.com/image/50565922110It seems here we just dive straight into the code.
In particular, I don't like so much coupling of the code and the html together, but that really depends what angle you are coming from, who is the target market of this library.
For example : Take KnockoutJS, it has really good separation of concerns. I really like the MVVM pattern, why? because it means if I have a team of HTML5/CSS design guys, I can let them work off just building good markup and focus on delivering quality static stuff, then my UI developers can work on custom binding handlers for nice CSS3 transition integration and then by C#/Java guys can get their paws wet implementing the data/server functionality without ever treading on each others toes.
I thorughly agree that functional programming concepts are very healthy for a program. Great functional Composability makes reusability and maintainability a dream.