-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
66 lines (55 loc) · 1.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// alias preact's hyperscript reviver since it's referenced a lot:
var h = preact.h;
// To use Classful Components in ES3/5, use your favorite inheritance technique.
// If you don't intend to use the Component class, you can skip this.
// Here's an example:
function createClass(obj) {
// sub-class Component:
function F(){ preact.Component.call(this); }
var p = F.prototype = new preact.Component;
// copy our skeleton into the prototype:
for (var i in obj) {
if (i === 'getDefaultProps' && typeof obj.getDefaultProps === 'function') {
F.defaultProps = obj.getDefaultProps() || {};
} else {
p[i] = obj[i];
}
}
// restore constructor:
return p.constructor = F;
}
/** Example classful component */
var App = createClass({
componentDidMount: function() {
this.setState({ message:'Hello!' });
},
render: function(props, state) {
return (
h('div', {id:'app'},
h(Header, { message: state.message }),
h(Main)
)
);
}
});
/** Components can just be pure functions */
var Header = function(props) {
return h('header', null,
h('h1', null, 'App'),
props.message ? h('h2',null,props.message) : null
);
}
/** Instead of JSX, use: h(type, props, ...children) */
var Main = createClass({
render: function() {
var items = [1,2,3,4,5].map( function(item) {
return h('li', {id:item}, 'Item '+item);
});
return (
h('main', null,
h('ul', null, items)
)
);
}
});
preact.render(h(App), document.body);