Last active
March 29, 2018 22:50
-
-
Save kristoferjoseph/aa52d899aecb29523b91b0f5c2666dd7 to your computer and use it in GitHub Desktop.
Modern Preact Component without class
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
var preact = require('preact') | |
module.exports = function Component (name, obj) { | |
name = name || 'F' | |
obj = obj || {} | |
var fn = function () { preact.Component.call(this) } | |
Object.defineProperty(fn, 'name', { value: name }) | |
fn.prototype = Object.create(preact.Component.prototype) | |
fn.prototype = Object.assign(fn.prototype, obj) | |
if (obj.getDefaultProps && | |
(typeof obj.getDefaultProps === 'function')) { | |
fn.defaultProps = obj.getDefaultProps() | |
} | |
fn.prototype.constructor = fn | |
return fn | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage