Skip to content

Instantly share code, notes, and snippets.

@bjconlan
Last active February 11, 2017 06:30
Show Gist options
  • Save bjconlan/49992bcedcd40c596f52af7892279c84 to your computer and use it in GitHub Desktop.
Save bjconlan/49992bcedcd40c596f52af7892279c84 to your computer and use it in GitHub Desktop.
VDomify builder function example for Inferno which allows JSON persistable template structures
<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