Skip to content

Instantly share code, notes, and snippets.

@iddan
Created April 13, 2017 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iddan/93c1c435307b932420d72b3a3472b1f0 to your computer and use it in GitHub Desktop.
Save iddan/93c1c435307b932420d72b3a3472b1f0 to your computer and use it in GitHub Desktop.
react functional
function component(...args) {
const [func, opts = {}] = args.reverse();
if (opts.constructor !== Object) {
throw new Error('Options must be a plain object');
}
class FunctionalComponent extends (opts.impure ? React.Component : React.PureComponent) {
constructor() {
super(...arguments);
this.setState = this.setState.bind(this);
}
render() {
return func(this.props, this.state, this.setState, this.linkEvent);
}
}
FunctionalComponent.displayName = func.name;
for (const opt in opts) {
if (typeof opts[opt] === 'function') {
FunctionalComponent.prototype[opt] = function (props, nextProps) {
console.log(props, nextProps);
return opts[opt](...arguments, this.setState, this.state, this.props);
};
} else {
FunctionalComponent.prototype[opt] = opts[opt];
}
}
return FunctionalComponent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment