- 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/...
> 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. :)