Re:React: Mixins Considered Harmful
by danabramov on Thursday July 14, 2016 @09:04AM
(#12093353)
In our experience inheritance causes a lot of hard-to-find issues, and muddles boundaries between component concerns. Some of the same problems I described we had in mixins also apply to inheritance.
>and you lose the nice function-composition-like behaviour provided by `super.method`.
We found this to be an anti-pattern. It’s easy to get lost in virtual calls across hierarchy, and people are going to create virtual methods (and override them) if you allow it.
>Instead you get hacks like `hoist-non-react-statics` to copy them over manually.
An alternative is to just not put static methods on component. It’s not such a useful feature.
For context, the article advocates the following:
This can be rewritten to remove the fixed dependency on `GlobalClass`, while preserving the underlying type: The only requirement is to maintain the contract outlined by the `SuperClass`, but that's what `super` is for. (By that I mean e.g. that `componentDidMount` should't be overwritten without also calling super.componentDidMount() before/after).Returning a different kind class entirely limits the utility of the pattern in general (IMO), and you lose the nice function-composition-like behaviour provided by `super.method`.