React v0.13.0 Beta 1
facebook.github.io
facebook.github.io
The change is subtle and easy to misinterpret. It's not that React drank ES6 class kool-aid and we're now gonna use inheritance over composition. (Nope[1], in fact pretty much the opposite[2].)
The real change is that React.createClass is no longer the way to create components. It's just a fancy wrapper (which is not even being deprecated). You want magic, you opt-in to it.
ES6 classes are also not the way to create components. As mentioned at the end of the article, you can even use ES3 module pattern:
function MyComponent(initialProps) {
return {
state: { value: initialProps.initialValue },
render: function() {
return <span className={this.state.value} />
}
};
}
React.createClass stops being special/required and becomes a (handy) utility. Can potentially be moved into a separate package.Competition for mixin systems is now possible.
Finally, this change opens up more possibilities for potential React-like frameworks to “interpret” React components. Of course we don't do that today, but it's still a nice property and may be handy later when you decide to switch to future React-like competitor.
It's the anti-lock-in.
[0]: http://jlongster.com/Modularity
[1]: https://github.com/facebook/react/issues/613#issuecomment-29...
[2]: https://github.com/reactjs/react-future/blob/master/01%20-%2...
They are moving out `setState` into a sideways module[1] so I assume full-featured classes without React.Component should be possible in the future.
[1]: https://github.com/facebook/react/commit/ed7332c74921874cdcb...
https://github.com/facebook/react/pull/2975#issuecomment-729...
However, if you want to use e.g. TypeScript with React, than this is a very big thing! Up until now, it was always a bit of a hassle, and with this change, it's completely straightforward (especially if you don't want JSX). You'll also need propTypes less because you can just use TypeScript interfaces for the `props` constructor parameter. Plus, TypeScript already has property initializers and all that, so all the other annoyances that you get when doing this with plain ES6 classes disappear with TypeScript.
I'm currently really considering porting my current project back to TypeScript for this single reason. The bad mix of TypeScript and JSX is what mostly holds me back. I'd like my view code to be somewhat understandable/hackable by designers and JSX really matters there. Any suggestions?
1. https://github.com/jbrantly/ts-jsx-loader (for webpack, but basic approach works in gulp/grunt as well)
Given the improvements in ES6, the fact that the JS community in general is so active, and the fact that I've seen some truly awful TS code (mainly where the devs want to pretend the web doesn't exist) I've been put off exploring TS too far much (beyond looking at the basic language features).
I'm not sure if I should be rethinking though?
I agree. Typescript is an impressive project, but most of the Typescript code I see looks more like C# or Java than Javascript. Sadly, it seems to have been created to accommodate that kind of approach.
But there's a lot to be said for strong and static typing when dealing with large projects.
That's why I'm hopeful for Flow, Facebook's new project to add static typing to Javascript:
Anyway totally agree on the typing angle, navigation/refactoring/discover-ability aspects are useful and I loved the implicit interface approach in Dart. However I wonder whether some of those features won't be added to JS over time (where possible in such a dynamic language). Ta for link to flow, will give it a look thanks.
There are a couple of links to editor syntax files and a link to WebStorm but they're a bit of an after thought.
I keep expecting the homepage update "any day now" but so far I've only really seen VS mentioned as a default environment.
One of the things I like about angular is how it deals with standard HTML. HAML doesn't care about its wacky attributes. Whats the best way to achieve this with react? Does the JSX compiler have hooks for preprocessing of any kind?
div
className: 'navbar-form'
style:
backgroundColor: "green"
,
div
className: 'btn-group'
,
button
type: 'button'
className: 'btn btn-warning'
onClick: @handleDecreaseWpmClick
,
span
className: 'glyphicon glyphicon-chevron-down'
span
className: 'btn btn-default disabled'
,
"#{ @props.status.get('wpm') }"
span
className: 'hidden-xs'
,
" wpm"
button
type: 'button'
className: 'btn btn-warning'
onClick: @handleIncreaseWpmClick
,
span
className: 'glyphicon glyphicon-chevron-up'
(For the curious, "wpm" is short for "words per minute" - this is straight out of my code for http://splashreaderapp.com. More code at http://github.com/rattrayalex/splashreaderapp) div
className: 'navbar-form'
style:
backgroundColor: "green"
div
className: 'btn-group'
button
type: 'button'
className: 'btn btn-warning'
onClick: @handleDecreaseWpmClick
span
className: 'glyphicon glyphicon-chevron-down'
span
className: 'btn btn-default disabled'
"#{ @props.status.get('wpm') }"
span
className: 'hidden-xs'
" wpm"
button
type: 'button'
className: 'btn btn-warning'
onClick: @handleIncreaseWpmClick
span
className: 'glyphicon glyphicon-chevron-up'Best place to start I guess would be to look into the JSX compiler to see how complicated that would be.
I've worked with C++ code bases where inheritance went crazy, and also with C++ code bases where composition via components was well supported and somewhat enforced. I'd love to see ES building in some kind of default support for composition with components if mixins are out.
See also how Unity3D does it: http://docs.unity3d.com/Manual/UsingComponents.html
I'm very happy to see JavaScript advancing, but I really hope we don't end up falling into the same holes other OOP languages fell into and then had to slowly educate and build themselves back out of again over the years. Frontend seems to be very good at rediscovering the old-new.
E.g. implementation: SubComponent (or something named better), a component can add the subcomponents to itself on initialization, and the lifecycle events can be similar to the Unity3D's SendMessage, i.e. similar to mixins. I think it would work quite well!
You would need to get the child subcomponent specifically e.g. get the timer subcomponent to create a timer, but as a bonus the subcomponent would not edit the instance of the component, feels cleaner TBH. Also two subcomponents can have the same this.field or this.property, or this.func without worrying about whether it belongs to the Component, another mixin, or something else :D
https://github.com/reactjs/react-future/blob/master/01%20-%2...
ES6 classes aren't "classic OOP", they're syntax sugar for regular prototypal inheritance that JavaScript has had forever.
The point is moving mixin handling to userland (and eventually some sort of standard?), if I understand correctly. It's not really React's concern.
See also: Minimal API Surface Area http://www.youtube.com/watch?v=4anAwXYqLG8
I deeply regret the day when all job postings for Javascript say "Looking for an object-oriented Javascript developer", since like lemmings companies think object-oriented programming is a good thing.
Only MVC described in object-oriented terms is "hard".
view(controller(model0,event0)) = representation
React's position is to avoid inheritance in favor of composition: https://github.com/facebook/react/issues/613#issuecomment-29...
This hasn't changed.
The change in 0.13 has nothing to do with inheritance though.
It's neither about classes nor about ES6. I wrote a comment explaining why: https://news.ycombinator.com/item?id=8959691
Still, perhaps we'll learn more about the roadmap and goals of the team at the React conference this week.
Also, is 40kb gzipped really something to lose sleep over? Especially when the mere act of moving from old jQuery/Backbone soup to React reduces the size of my real code by a far greater amount.
It's my (perhaps flawed) understanding that React deals only with the "view" part of an app, and that you can use Backbone and React concurrently.
Could you elaborate on what you call "Backbone soup" and how React helps solve this problem?
I was building a particularly complicated content editor with Backbone, and was getting frustrated. So I spent the next morning learning React, then re-implemented the whole thing that afternoon. All the complexities I was grappling with disappeared. Obviously there were new challenges to replace the old ones, but we've found it's much easier for someone to pick up work on a React project than it ever was with Backbone.
To start with we were using Backbone models with React views, but now we just use a flux implementation (Fluxxor), and don't bother with much else. Our client-side JS stack is essentially React, Immutable, Fluxxor, superagent, moment and mousetrap, with only the first three being core ingredients.
We gradually moved from Backbone to React+Backbone, and finally to React+Flux. It's been amazing since.
The way we see it, ES6 and 7 are around the corner and then the createClass dependency becomes optional.
https://gist.github.com/AndrewIngram/f0574f79fca8e13e201b
It's a similar pattern to how I got Backbone models and views converted to ES6 classes.
So, for example, calling a function f(a, b) is first done normally, and then, when e.g. "b" changes value, the function f(a, b) is re-evaluated incrementally. Note that this means that not simply f is invoked again, but that f is recursively re-evaluated.
Anybody here aware of such a system?
I know there is research in this area ([1]). I would really like to use such a system in Javascript.
[1] http://www.umut-acar.org/publications-by-topic#TOC-Self-Adju...
While it sounds like an awesome idea, things get crazy really fast. It's hard to explain briefly, but make it explicit and clear to the programmer which things are reactive streams and which are simply values makes it much easier to predict what the program is doing.
If my program just gets re-evaluated in an optimal way (without any other side-effects) then that would make my life much simpler.
The research I linked to already states that it can be done. So I was wondering if there are any Javascript implementations yet out there.
Check out Adapton though, which is trying to bridge the gap.
I just hope there will be a browser-implementation soon :)
Something like,
this.handler = new Handler(window, "mouseup", () => this.whatever());
// later...
this.handler.detach();
YMMV though. render: ->
div(onClick: @tick, 'Clicks: ', @state.count)
I couldn't find any transformers which support it. Is there anyone out there?In fact, they're doing the opposite: deprecating proprietary pseudo-OOP `React.createClass` helper and let people use whatever they fancy. This makes it easier to write React components using idiomatic constructs from different languages, whether it is TypeScript, ClojureScript, CoffeeScript, or whatever. Also lets you use any mixin system, if you need it at all.
React's stance on inheritance: https://github.com/facebook/react/issues/613#issuecomment-29...
“Minimal API surface area”: http://www.youtube.com/watch?v=4anAwXYqLG8
We're definitely considering optional module based systems instead of OOP, but it's still too early. https://github.com/reactjs/react-future/tree/master/07%20-%2...