React on ES6+
babeljs.io
babeljs.io
class OuterComponent extends React.Component {
render() {
return (
<MixinComponent>
<InnerComponent />
</MixinComponent>
);
}
}
Being replaced by something more like: class OuterComponent extends React.Component {
render() {
return JSX`
<MixinComponent>
<InnerComponent />
</MixinComponent>
`;
}
}Since JSX is just JavaScript, it doesn't have any problems with nesting.
`${`test`}` However, this would lead to further divergence. Tooling that is built around the assumptions imposed by template literals wouldn't work. It would undermine the meaning of template literals. It would be necessary to define how JSX behaves within the rest of the ECMAScript grammar within the template literal anyway.
I'm not really what the problem is here. Template strings seem to have been put in ES6 partly to do what JSX does, so it seems very odd that anyone went to the lengths of defining a language extension rather than use them.More importantly, how would you share data structures? `<Todos items=${someObject}>` would return a string with the string representation of someObject, but it wouldn't be the same object, have methods, etc.
<MixinComponent>
<InnerComponent name='madeofpalk'/>
</MixinComponent>
JSX will convert that into regular JS which will look like element('MixinComponent', {},
element('InnerComponent', {name: 'madeofpalk'})
)
Parsing that string during runtime to produce the above data structure isn't ideal.I'm genuinely curious about all the ES6/ES7 articles. Is the interest driven from the eventual native support in future browsers and being able to drop transpiler dependencies?
{div, a} = React.DOM
...
render: ->
(div {},
(a {href: 'foo'}, 'bar')
)
The only downside is having to extract the tags from React.DOM. MyComponent = React.createFactory require './my-component'
then you can do render: ->
(div {},
(MyComponent {})
)I haven't had any issues without the parens, but then again I've only just fooled around.
You probably know better.
ES6 improves the language without borking the syntax so much.
To add something more constructive, CoffeeScript is still just a transpiler. I think the long-term goal is for browsers to "natively" interpret/run ES6 code rather than forever transpiling to ES5. Either that, or we actually move on to something like web assembly.
That's not an opinion; this will actually happen. See this: https://kangax.github.io/compat-table/es6/
If there were something like CoffeeScript+JSX for LiveScript I would be sold.
Doesn't ES6 still require transpiling for maximum compatibility at this point? I would reverse your statement; aside from polyfills, ES6 offers me very little advantage over CS.
edit: Someone else posted that https://github.com/jbhatab/middleman-backbone-react-template...
Everytime I see people saying bad things about coffeescript I really wonder why. This looks way nicer/clearer to me.
[1] https://facebook.github.io/react/blog/2015/01/27/react-v0.13...
1. https://github.com/STRML/react-router-component/blob/master/...
> The only limitation I see now is a mixin that defines more mixins; this currently doesn't work
Why and how would you do this?
1. https://github.com/STRML/react-router-component/blob/master/... 2. https://github.com/STRML/react-router-component/blob/master/...
Speaking more to the anti-pattern part of your comment, obviously whether or not you think they are hard to read is subjective.
I find inheritance much harder to reason about or even read. Mixins don't always lead to the clearest code, but they're better than all the alternatives.
The main issue with mixins is that the origin of behavior becomes opaque. If I call this.mixinFunction(), it's not defined in my file or imported directly, so I have to know exactly which functions come from which mixins. With higher-order components, you can just look at the props and know where things originated.
I'm not a staunch defender of any particular approach, but I think that's the main argument against mixins. (And it applies to other languages as well, such as Ruby.)
import Foo from './foo';
let Bar == React.createClass({});
Bar = Foo(Bar);
Bar.mixinFunction();
and: import Foo from './foo';
let Bar == React.crateClass({mixins: Foo});
Bar.mixinFunction();
In both cases I'm adding functions to my Bar component; in neither case is it defined in my file or added directly.It is true that in the general case the mixin approach would mutate the internal state of Bar, whereas the higher-order component approach would re-render Bar with updated props, which is a solid win for the latter style. (Conversely, a mixin can check the state of the underlying component, while a higher-order component cannot, which means that you can't really implement shouldComponentUpdate as a higher-order component.)
But it's really a fairly subtle difference, and I wouldn't say that either is particularly more transparent. :)
- Use old syntax or explicitly add mixin to prototype - Use composition. A bit annoying, because you have to make an extra class
class OuterComponent extends React.Component {
render() {
return (
<MixinComponent>
<InnerComponent />
</MixinComponent>
);
}
}
class InnerComponent extends React.Component{}
- wait for decorators (?) $ babel --optional es7.decorators
or babel.transform("code", { optional: ["es7.decorators"] });
Here[2] is an article that describes how to use it for mixinsBy the way, TypeScript recently (in nightly) gained support for JSX, so we can now have all of this ES6 stuff + types on top of it.
The idea is you declare which components will be needed and the library takes care of instantiating objects with the necessary components for you. It works something like Unity (the game engine)'s entity-component architecture.
I haven't tried to get it working with React yet though, I've been playing with threejs instead :)
https://www.npmjs.com/package/mixin-decorator
edit: a link would be helpful, right?
[0] https://github.com/facebook/nuclide
[1] https://github.com/facebook/nuclide/tree/master/pkg/nuclide/...
I recommend avoiding gulp and grunt and any other build systems. They're completely unnecessary. npm and webpack are sufficient. npm works well as a simple task runner, and there are webpack plugins to do pretty much anything you'd ever do with grunt or gulp.
Just took me half an hour from start to getting react+ES6 running.
[0] http://webpack.github.io/docs/tutorials/getting-started/I'm currently studying computer science in germany. In a team of 5 we created the above project for a software engineering assignment. But in the end I wrote the entire code part alone. :D
It implements a simulated elevator and displays the internal state in a UML state machine diagram.
It features:
- Grunt build system
- JSX + ES6 to ES5 transpiler using babelify and browserify
- LESS to CSS transpiler
- karma + jasmine test system
- live reloading ("grunt serve")
- building a static page from the source code ("grunt dist")
- building a nw.js based app from the static page ("grunt build")It allows you to immediately start writing ES6 code for React without the spending hours setting everything up.
It's a work in progress, and any feedback would be appreciated!
https://github.com/gaearon/redux/tree/master/examples/todomv...
It provides an environment for development, testing, and production environments. In development, hotloading / live reload of assets when possible, and refresh otherwise. In production it'll do proper cache busting and asset minification. While testing it generates code coverage!
Best part? No gulp or grunt. It's all basically a heavily commented webpack config and a few npm scripts.
All of the people that have responded to you so far seem to disregard at least one of the details that I consider important. Mainly, it should have all the tasks needed for a web app that you plan on working on for a period longer than a few days, and it shouldn't make you deal with more build tool bullshit than is necessary.
I don't have any examples of big projects using web-app without modifications, however, I have a small example: quad-blog [2], which is my blog. It's written with fluxible, and the webpack config for that project is based on web-app. It takes it a step further and does isomorphic (or universal) javascript, so it'll do server-side rendering and then delegate to the client once it's loaded. One other highlight of the project is that it uses Koa, so you get real middleware (as in, middleware that behaves like a stack). Oh, and it uses Sequelize and shows how you'd do database migrations with an awesome relational database: Postgres, instead of taking the NoSQL shortcut (which is woefully inadequate for a lot of applications).
[0] http://github.com/cesarandreu/web-app
[1] https://blog.cesarandreu.com/posts/a_reasonable_starting_poi...
Check it out: http://react-in-meteor.readthedocs.org/en/latest/
(I work at Meteor)
Here's a tutorial on how somebody integrated webpack, react, and rails.
Honestly I hate the react-rails gem. Whoever was maintaining it kept doing things at the whims of issue requestors. I remember one instance where they broke our apps with an upgrade because someone decided it would be a good idea to shim a definition for `window` into server prerenders. All because an issue creator who didn't really know what they were doing supposedly "needed" that to get browserify-rails working.
So we made this: https://github.com/revelrylabs/execjs-rails. That handles the server-side rendering for us. We only use react-rails for its assets, never for its helpers.
And here's our own (always-evolving) layer on top of execjs-rails: http://toolkit.revelry.co/pages/core.
Examples are written in dusty JavaScript, but we've successfully used revelry_core with sprockets-es6 several times now.
EDIT: Still no modules yet. It doesn't help that Josh Peek deleted the old sprockets-es6 repo and all its issues before passing off maintenance to someone else.)
Say React-router, how would you use that if you're extending a class in ES6 and need to rely on that mixin?
A lot of libraries, including react-router, are considering decorators as an alternative.[2]
[1] http://facebook.github.io/react/blog/2015/01/27/react-v0.13....
Not to mention the issues with all the god damn different build tools, it gets hard to get something standardised and good.
> Right away, you'll notice a subtle difference – a more terse syntax is available to you when defining classes:
// The ES5 way
var Photo = React.createClass({
handleDoubleTap(e) { … },
render() { … },
});
// The ES6+ way
class Photo extends React.Component {
handleDoubleTap(e) { … }
render() { … }
}
It may or may not be more terse in longer examples, but ES6 already supports things to make the class syntax less terse here by about the same margin the article claims.