-
-
Save jbondc/b09d56dc4b8ed6c43af6 to your computer and use it in GitHub Desktop.
Trait vs. mixin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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