Writing hyperscript can get tedious when repeating this syntax for every element.
h('div', 'this is an element', [
h('ul', [
h('li', 'writing all these h calls')
h('li', 'can get boring...')
])
])
.Media { | |
display: flex; | |
align-items: stretch; | |
} | |
.Media__image { | |
margin-right: 1em; | |
} | |
.Media__body { |
function extend(base, derivative){ | |
for(var i in base) | |
if(!derivative[i]) | |
derivative[i] = base[i] | |
return derivative | |
} | |
var base = { | |
color: '#555', | |
fontSize: '2em' |
function styles(padding){ | |
return { | |
padding: padding + 'px', | |
backgroundColor: '#ddd' | |
} | |
} | |
function Hello(name, styles){ | |
return ["div", {style:styles}, "Hello " + name] | |
} |
function Hello(name){ | |
return ["div", null, | |
["b", null, "Hello "], | |
["i", null, name] | |
]; | |
} | |
function prerender(root){ | |
for(var i in root) | |
if(Array.isArray(root[i])) |
function Hello(name){ | |
return ["div", null, "Hello " + name]; | |
} | |
ReactDOM.render( | |
React.createElement.apply(this, Hello('world')), | |
document.getElementById('container') | |
); |
function Hello(name){ | |
return React.createElement("div", null, "Hello " + name); | |
} | |
ReactDOM.render( | |
Hello("World"), | |
document.getElementById('container') | |
); |
// https://jsfiddle.net/reactjs/5vjqabv3/ | |
var Hello = React.createClass({ | |
displayName: 'Hello', | |
render: function() { | |
return React.createElement("div", null, "Hello ", this.props.name); | |
} | |
}); | |
ReactDOM.render( |
function mergeObjects(merger,mergee){ | |
var mergent = {}; | |
for(var prop in mergee){ | |
mergent[prop] = mergee[prop] | |
} | |
for(prop in merger){ | |
if(mergent[prop]) | |
mergent[prop] = mergeObjects(mergent[prop],merger[prop]) | |
else mergent[prop] = merger[prop] |
Writing hyperscript can get tedious when repeating this syntax for every element.
h('div', 'this is an element', [
h('ul', [
h('li', 'writing all these h calls')
h('li', 'can get boring...')
])
])
This was a small challenge to write a standard and unstyled todos app within 100 lines of code, while maintaining readability.
It can be found on JS Bin
This project was to also prove that components are nothing more than a function that returns a vTree. The only purpose of creating an instance of the component is to maintain internal state. If there is none, then a component can just be a pure function.
Although a trivial application, it might also demonstrate that such things can be built without frameworks or virtual-dom libraries - only the virtual-dom is needed.
I did use my own fork from the virtual-dom repo for this project so that I could use the vDOM object.