4 ms·
It's funny React gets bashed in this thread for pushing out createClass crutch, mixins, autobinding into userland, when usually it is being modularity shamed[0]
by danabramov 12y ago
It's funny React gets bashed in this thread for pushing out createClass crutch, mixins, autobinding into userland, when usually it is being modularity shamed[0].
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 http://jlongster.com/Modularity
[1]: https://github.com/facebook/react/issues/613#issuecomment-29280175 https://github.com/facebook/react/issues/613#issuecomment-29...
[2]: https://github.com/reactjs/react-future/blob/master/01%20-%20Core/03%20-%20Stateless%20Functions.js https://github.com/reactjs/react-future/blob/master/01%20-%2...
- tolmasky 12y agoGiven the ES3 style creation if components (which appears to have no "superclass"), can you also just do "class MyComponent" (no "extends React.Component"?)
- danabramov 12y agoI think so, but you'll probably miss `this.setState`. 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/ed7332c74921874cdcb3cce629bb5bd8c95a7968 https://github.com/facebook/react/commit/ed7332c74921874cdcb...
- danabramov 12y agoIndeed, it is not necessary to inherit form React.Component, see confirmation: https://github.com/facebook/react/pull/2975#issuecomment-72962088 https://github.com/facebook/react/pull/2975#issuecomment-729...
- deleted 12y ago[deleted]