- <html>
- <head></head>
- <body>
- <div id="abcd"></div>
- <script src="https://npmcdn.com/preact@2.8.3/dist/preact.js"></script>
- <script src="https://npmcdn.com/preact-compat@0.6.1/preact-compat.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.min.js"></script>
- <script>
- window.React = preactCompat;
- window.ReactDOM = preactCompat;
- </script>
- <script type="text/babel">
- const { h, render, Component } = preact; // normally this would be an import statement.
- class Clock extends Component {
- constructor() {
- super();
- // set initial time:
- this.state.time = Date.now();
- }
- componentDidMount() {
- // update time every second
- this.timer = setInterval(() => {
- this.setState({ time: Date.now() });
- }, 1000);
- }
- componentWillUnmount() {
- // stop when not renderable
- clearInterval(this.timer);
- }
- render(props, state) {
- let time = new Date(state.time).toLocaleTimeString();
- return <span>{ time }</span>;
- }
- }
- render(<Clock/>, document.getElementById('abcd'));
- </script>
- </body>
- </html>
Reference:
No comments:
Post a Comment