3 ms·
That's too bad, mixins were simple to grasp and worked really well. The oft suggested alternative to mixins is higher order components. Here's one of my favorit
by jameslk 10y ago
That's too bad, mixins were simple to grasp and worked really well. The oft suggested alternative to mixins is higher order components. Here's one of my favorite quotes[0] on the matter:
> You lose so much with [higher order components], especially with es6:
> You lose the (original) class, and with it, the ability to compose it, to extend it and …. reflection. All your components are [the higher order component]. For example, you cannot rely on the Class.name in es6.
> You lose the ability to extend the component and expose extra members. Your higher order component will not ‘pass’ that through.
> The above two basically renders the ability to compose several decoupled and agnostic one to the other “higher level components” impossible.
> It is verbose and non-declarative , and basically much less readable and maintainable.
If there's a better pattern than mixins, I would say its traits[1]. For some reason, I don't find it used much often in the wild. I'm not sure it would be practically applied to React anyway without ending up with mixins again, but at least the concept would be clearer: there should be no shared state and dependencies are explicit.
0. https://medium.com/@danikenan/you-lose-so-much-with-your-solution-especially-with-es6-bcf4fdad40be#.u0e6qabr1 https://medium.com/@danikenan/you-lose-so-much-with-your-sol...
1. https://en.wikipedia.org/wiki/Trait_%28computer_programming%29 https://en.wikipedia.org/wiki/Trait_%28computer_programming%...
- Vilkku 10y agoFrom the article: > Let’s make it clear that mixins are not technically deprecated. If you use React.createClass(), you may keep using them. We only say that they didn’t work well for us, and so we won’t recommend using them in the future.
- danabramov 10y ago>the ability to compose it, to extend it and …. reflection In our experience those are precisely the things that can turn codebases into a mess. It’s great that you can manage the complexity from inheritance and reflection, but in our experience most people had problems with these patterns (me included). On the other hand, we found that React composition model is enough to express everything we needed in the apps, and we didn’t miss inheritance or reflection.