Skip to content

Instantly share code, notes, and snippets.

@bzar
Created January 26, 2015 18:04
Show Gist options
  • Save bzar/9a2a191e24e495cdbf1f to your computer and use it in GitHub Desktop.
Save bzar/9a2a191e24e495cdbf1f to your computer and use it in GitHub Desktop.
function E(node) {
function isChildren(x) { return x instanceof Array; }
function isText(x) { return typeof x === "string"; }
if(!(node instanceof Array))
return node;
var element = node[0];
var props = null;
var text = null;
var children = null;
if(node.length > 1) {
if(isChildren(node[1])) {
children = node[1].map(E);
} else if(isText(node[1])) {
text = node[1];
if(isChildren(node[2])) {
children = node[2].map(E);
}
} else if(node.length == 2) {
props = node[1];
} else if(isChildren(node[2])) {
props = node[1];
children = node[2].map(E);
} else if(isText(node[2])) {
props = node[1];
text = node[2];
if(isChildren(node[3])) {
children = node[3].map(E);
}
}
}
return React.createElement(element, props, text, children);
}
<!doctype html>
<html>
<head>
<script src="react.min.js"></script>
<script src="E.js"></script>
<style>
.fancy { color: pink; }
</style>
</head>
<body>
<div id="ui" />
<script src="index.js"></script>
</body>
</html>
var subcomp = E(['blockquote', 'Awesome!'])
var rootClass = React.createClass({
getInitialState: function() {
return {text: ""};
},
onChange: function(e) {
this.setState({text: e.target.value});
},
render: function() {
return E(['div', [
['h1', 'Hello, world!'],
['p', {className: 'fancy'}, this.state.text],
subcomp,
['input', {onChange: this.onChange}]
]]);
}
});
React.render(React.createElement(rootClass), document.getElementById('ui'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment