-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo.js
70 lines (57 loc) · 2.37 KB
/
demo.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
67
68
69
70
var h = preact.h;
var Header = function (props, context) {
return h('header', null, h('h1', null, props.prefix + props.name));
};
Header.defaultProps = { name: '头部' };
var Input = preact.createClass({
componentWillMount: function () { console.log('Will-Input'); },
componentDidMount: function () { console.log('Did-Input'); },
render: function (props, state) {
return h('input', { type: 'text', value: props.inputStr, oninput: props.inputCall });
}
});
var Bottom = preact.createClass({
componentWillMount: function () { console.log('Will-Bottom'); },
componentDidMount: function () { console.log('Did-Bottom'); },
getInitialState: function () {
return { inputStr: 'ddd' };
},
inputCall: function (val) {
this.setState({ inputStr: val.target.value });
},
render: function (props, state) {
return h('div', null, h('label', null, props.prefix), h(Input, { inputStr: state.inputStr, inputCall: this.inputCall.bind(this) }), state.inputStr);
}
});
var Content = preact.createClass({
componentWillMount: function () { console.log('Will-Content'); },
componentDidMount: function () { console.log('Did-Content'); },
render: function (props, state) {
var items = [1, 2, 3, 4, 5].map(function (item) {
return h('li', { id: item }, props.name + props.children[0] + item, props.children[1]);
});
return h('ul', null, items);
}
});
var Body = preact.createClass({
componentWillMount: function () { console.log('Will-Body'); },
componentDidMount: function () { console.log('Did-Body'); },
render: function (props, state) {
return h(Content, { name: props.prefix }, 'item', h(SubA));
}
});
var SubA = preact.createClass({
componentWillMount: function () { console.log('Will-SubA'); },
componentDidMount: function () { console.log('Did-SubA'); },
render: function (props, state) {
return h('a', { href: 'https://www.baidu.com/' }, '链接');
}
});
var App = preact.createClass({
componentWillMount: function () { console.log('Will-App'); },
componentDidMount: function () { console.log('Did-App'); },
render: function (props, state) {
return h('div', null, h(Header, { prefix: 'head:' }), h(Body, { prefix: 'body-' }), h(Bottom, { prefix: 'bottom:' }));
}
});
preact.render(document.getElementById('why'),h(App));