Created
April 13, 2017 07:24
-
-
Save iddan/93c1c435307b932420d72b3a3472b1f0 to your computer and use it in GitHub Desktop.
react functional
This file contains hidden or 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
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