260 points · by clessg · 11y ago · 93 comments
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>
`;
}
} 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.
`${`test`}`Since JSX is just JavaScript, it doesn't have any problems with nesting.
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?
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/
{div, a} = React.DOM
...
render: ->
(div {},
(a {href: 'foo'}, 'bar')
)
The only downside is having to extract the tags from React.DOM.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.
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.
- 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.
https://www.npmjs.com/package/mixin-decorator
edit: a link would be helpful, right?
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 :)
[1] https://facebook.github.io/react/blog/2015/01/27/react-v0.13...
1. 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.
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.
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 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...
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!
Check it out: http://react-in-meteor.readthedocs.org/en/latest/
(I work at Meteor)
https://github.com/gaearon/redux/tree/master/examples/todomv...
Just took me half an hour from start to getting react+ES6 running.
[0] http://webpack.github.io/docs/tutorials/getting-started/[0] https://github.com/facebook/nuclide
[1] https://github.com/facebook/nuclide/tree/master/pkg/nuclide/...
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.)
Here's a tutorial on how somebody integrated webpack, react, and rails.
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.