Skip to content

Instantly share code, notes, and snippets.

@kristoferjoseph
Last active March 29, 2018 22:50
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 kristoferjoseph/aa52d899aecb29523b91b0f5c2666dd7 to your computer and use it in GitHub Desktop.
Save kristoferjoseph/aa52d899aecb29523b91b0f5c2666dd7 to your computer and use it in GitHub Desktop.
Modern Preact Component without class
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
}
@kristoferjoseph
Copy link
Author

kristoferjoseph commented Mar 29, 2018

usage

return Component(
  'MyComponent', 
  {
    state: {
      title: '',
      stuff: []
    },
    render (props, state) {
      return h('h1', props, this.state.title)
    }
  }
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment