Created
April 24, 2015 03:13
-
-
Save angus-c/df24a9af6e99b4b5398f to your computer and use it in GitHub Desktop.
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
/* | |
* based on https://gist.github.com/sebmarkbage/fac0830dbb13ccbff596 | |
* by Sebastian Markbåge | |
*/ | |
import React from 'react'; | |
const noop = () => {}; | |
const es6ify = (mixin) => { | |
if (typeof mixin === 'function') { | |
// mixin is already es6 style | |
return mixin; | |
} | |
return (Base) => { | |
// mixin is old-react style plain object | |
// convert to ES6 class | |
class NewClass extends Base {} | |
Object.assign(NewClass.prototype, mixin); | |
return NewClass; | |
}; | |
}; | |
function mixin(...mixins) { | |
class Base extends React.Component {} | |
// No-ops so we need not check before calling super() | |
['componentWillMount', 'componentDidMount', 'componentWillReceiveProps', | |
'shouldComponentUpdate', 'componentWillUpdate', 'componentDidUpdate', | |
'componentWillUnmount', 'render']. | |
forEach(m => Base.prototype[m] = noop); | |
mixins.reverse(); | |
mixins.forEach(mixin => Base = es6ify(mixin)(Base)); | |
return Base; | |
} | |
export default mixin; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment