-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.js
41 lines (37 loc) · 893 Bytes
/
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
/** @jsx React.h */
import React from './src/preact.mjs';
class ChildComponent extends React.Component {
render() {
return (
<Link href="/" >{this.props.children}</Link>
);
}
}
class Clock extends React.Component {
constructor() {
super();
// 设置初始时间
this.state.time = Date.now();
}
componentDidMount() {
// 每秒更新时间
this.timer = setInterval(() => {
this.setState({ time: Date.now() });
}, 1000);
}
componentWillUnmount() {
// 当节点被移除之后移除监听
clearInterval(this.timer);
}
render() {
let time = new Date(this.state.time).toLocaleTimeString();
return (
<ChildComponent >{time}</ChildComponent>
);
}
}
function Link({ children, ...props }) {
return <a {...props}>{children}</a>
};
// 渲染一个clock实例到 body中
React.render(<Clock />, document.body);