Created
November 14, 2016 03:52
-
-
Save matthewmueller/d2b7908666a05ba0042f86dd3e83bb88 to your computer and use it in GitHub Desktop.
vnode hook in action
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
/** | |
* 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