Why did we build React?
facebook.github.io
facebook.github.io
https://s3.amazonaws.com/uploads.hipchat.com/6574/26709/htv8...
(The right side is a live preview of what's on the left. The editor is open-source https://github.com/Khan/perseus but not yet documented, sorry.)
React made the code about 15% shorter and also made an order-of-magnitude improvement because it's smarter than I was being about figuring out what's changed and when it's necessary to touch the DOM. Of course, I could've made the same perf improvements by hand but it was easier and quicker having a library that just handles everything.
(This comment sounds a little like I'm getting paid to write this. I'm not! Just really hope that React takes off.)
> By unifying your markup with it's corresponding view logic
I need to see a real use case as well.If you had a scatterplot with thousands of points, being able to add or subtract a couple without re-calculating the whole thing would certainly interest me.
Rendering is incredibly quick, since React only touches the parts of the DOM that have changed. Components are very lightweight, so you can break down the UI in as many, self-contained parts as you like.
And you can do pretty much anything you like without ever having to use a single selector or DOM manipulation. That means that you only need classnames for CSS.
The only downside I've discovered so far is the extra tooling required to use the embedded HTML, and lacking compatibility with other tools, but I suppose that will become better with time.
It like developing with JQuery where you had something like:
$('.whatever').append('<div>Hello ' + this.props.name + '</div>');
and now with JSX you can do (minus the React structure around it):
<div>{'Hello ' + this.props.name}</div>
So... looks a bit nicer... less quotes...? But so what. I don't want to tightly couple my presentation to my logic.
I have no experience with React but using JSX (or Javascript) to manipulate the DOM doesn't necessarily mean your view is not decoupled from your controller or model.
I have been looking at Angular a lot lately and this is another example where view components are constructed from a mix of html(templates), the DOM (directives), and Javascript (the code that implements directives). The model is pure Javascript objects and the controller is also Javascript.
Decoupling is an abstract concept that can be realized in many different ways.
I've developed my app. Designer gets hired later and spits out a cool new layout and design... No feature changes at all.
similarly...
Then next year, management decides we need to give the app a "fresh new look" for v2.
[If I'm using React or JQuery-only] From what I can tell, this will cause me to have to dive through a whole bunch of JS code where I'm sifting through business logic and imperative code to find JSX/html sprinkled throughout and make the necessary changes. Also, since I'm changing actual code the chances of introducing new bugs in this process is high.
[If I'm using Angular] Go through html. Change html and css. Leave code and business logic alone.
This is not an entirely contrived situation (at least not for me). As I'm working towards a releasable product, one of the areas that go through the most change and flux is the design and layout.
Working with React components vs angular directives are pretty similar in terms of difficulty for designers, IMO, but we think that React components are easier to build than directives. Most designers can figure it out if they know html.
One criticism about facebook, google, etc is that the greatest minds are being used to make people click more ads. But they're open sourcing some interesting things so it's not all bad.
return <div>Hello {this.props.name}</div>
What is this returning? A string, or a var '<div>Hello {this.props.name}</div>'? Never seen js that looks like thisps. looks like a useful project
From the article: > The data returned from render is neither a string nor a DOM node -- it's a lightweight description of what the DOM should look like.
Like @thezilch said, the syntax is JSX, which is pre-processed into runnable JS.
If you're interested in using jQuery with React, we have an example: https://github.com/facebook/react/tree/master/examples/jquer.... It doesn't use XHR but it should give you some idea of how the 2 can play nicely together.
I think the reason that most people think this is a good separation of concerns is because they used WYSIWYG editors in the past (which are no longer used in large projects) or because they have PHP spaghetti code hangover from the 90s.
Mixing code and markup isn't evil in and of itself, it's mixin non-display concerns with display concerns which causes issues.
With React, you can use components to separate concerns with finer granularity and in ways that are more tailored to your application.
While I agree that other MVC frameworks are not great (I've seen many of Angular's pains & weaknesses), I think this was a step in the wrong direction.
http://jsfiddle.net/vla/Cdrse/(from https://medium.com/make-your-own-apps/e71bcedc36b) versus https://github.com/petehunt/react-tutorial/blob/master/scrip...