Skip to content

Instantly share code, notes, and snippets.

@matthewmueller
Created November 14, 2016 03:52
Show Gist options
  • Save matthewmueller/d2b7908666a05ba0042f86dd3e83bb88 to your computer and use it in GitHub Desktop.
Save matthewmueller/d2b7908666a05ba0042f86dd3e83bb88 to your computer and use it in GitHub Desktop.
vnode hook in action
/**
* Module dependencies
*/
const stringify = require('preact-render-to-string')
const preact = require('preact')
const h = preact.h
preact.options.vnode = function (node) {
if (!node.attributes || !node.attributes.class) return
if (typeof node.nodeName === 'function') return
node.attributes.class = node.attributes.class.toString()
}
/**
* Initialize `App`
*/
const App = (props) => (
h(Header, props)
)
const Header = (props) => (
h('div', { class: header() }, `hello ${props.name}!`)
)
function header () {
return {
toString: () => 'header'
}
}
/**
* Browser
*/
if (typeof document !== 'undefined') {
preact.render(App({ name: 'matt' }), document.body, document.body.firstChild)
} else {
console.log(stringify(App({ name: 'matt' })))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment