Skip to content

Instantly share code, notes, and snippets.

@angus-c
Created April 24, 2015 03:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angus-c/df24a9af6e99b4b5398f to your computer and use it in GitHub Desktop.
Save angus-c/df24a9af6e99b4b5398f to your computer and use it in GitHub Desktop.
/*
* 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