Last active
February 11, 2017 06:30
-
-
Save bjconlan/49992bcedcd40c596f52af7892279c84 to your computer and use it in GitHub Desktop.
VDomify builder function example for Inferno which allows JSON persistable template structures
This file contains 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
<html> | |
<head> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/inferno/1.0.7/inferno.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/inferno/1.0.7/inferno-component.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/inferno/1.0.7/inferno-create-element.js"></script> | |
</head> | |
<body> | |
<div id="app" /> | |
<script> | |
// Review Inferno.createElement dep. perhaps Inferno.createVNode would suffice. | |
Inferno.vdomify = function (node) { | |
if (typeof node === 'string') { | |
return node; | |
}; | |
let [name, attributes, children] = node; | |
return children && children.length | |
? Inferno.createElement.apply(this, [name, attributes].concat(children.map((c) => Inferno.vdomify(c)))) | |
: Inferno.createElement(name, attributes); | |
} | |
class MyComponent extends Inferno.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
counter: 0, | |
} | |
} | |
render() { | |
return Inferno.vdomify( | |
['div',,[ | |
['h1',,['Header']], | |
['span',,[`Counter is at: ${this.state.counter}`]]]]); | |
} | |
} | |
Inferno.render(Inferno.createElement(MyComponent), document.getElementById('app')); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment