-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-es6-mixins.js
42 lines (37 loc) · 1.3 KB
/
react-es6-mixins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
export default function ReactComponentWithMixins(...args){
let BaseClass = args.pop()
let mixins = args
// create base mixing class
class MixedComponent extends BaseClass {}
// find mixins that extend state
let mixinsWithState = mixins.filter((mixin)=>mixin.getInitialState)
// add state mixer to other mixins
let mixinsWithHelper = mixins.reverse().concat({
componentWillMount: function(){
for( let mixin of mixinsWithState) {
let mixinState = mixin.getInitialState()
for( let stateKey in mixinState) {
// overwrite state field only if class hasn't already set the key.
// mixins overwrite their states in order of applying
if(this.state[stateKey] === undefined) {
this.state[stateKey] = mixinState[stateKey]
}
}
}
}
})
// extend our mixed base class with mixins
for( let mixin of mixinsWithHelper ) {
for( let name in mixin ) if (name != 'getInitialState') {
let f = MixedComponent.prototype[name]
MixedComponent.prototype[name] = function(){
// take the result from the rightmost mixin
let r = mixin[name].apply(this,arguments)
// then call the rest if any
;(f && f.apply(this,arguments))
return r
}
}
}
return MixedComponent
}