Skip to content

Instantly share code, notes, and snippets.

@jbondc
Last active August 29, 2015 14:18
Show Gist options
  • Save jbondc/b09d56dc4b8ed6c43af6 to your computer and use it in GitHub Desktop.
Save jbondc/b09d56dc4b8ed6c43af6 to your computer and use it in GitHub Desktop.
Trait vs. mixin
// Using traits
// https://raw.githubusercontent.com/reactjs/react-future/master/01%20-%20Core/02%20-%20Mixins.js
// Chainable mixins
trait A {
componentDidMount() {
console.log('A');
}
};
trait B extends A {
static getQueries() {
console.log('B')
}
componentDidMount() {
console.log('B');
this[A].componentDidMount(); // This will end up calling A.componentDidMount
}
}
class C {
import A;
import B;
static getQueries() {
B.getQueries();
console.log('C');
}
componentDidMount() {
this[B].componentDidMount()
console.log('C');
}
}
C.getQueries(); // B, C
new C().componentDidMount(); // B, A, C
import { Component } from "react";
import { mixin } from "react-utils";
// A component that mixes in all of C's functionality
class MyComponent extends mixin(Component, C) {
render() {
return <div />;
}
}
// No issues
export class C {
import A;
import B;
state = {
b: true
}
componentDidMount() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment