Created
April 10, 2017 18:48
-
-
Save arackaf/a627097825ade10c0717f881903b6a4b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {render} from 'react-dom'; | |
| import React, {Component, Children, cloneElement} from 'react'; | |
| class CurrentTime extends Component { | |
| state = {time: ''}; | |
| componentDidMount() { | |
| this._interval = setInterval(() => this.setState({time: this.getTime()}), 50); | |
| } | |
| componentWillUnmount() { | |
| clearInterval(this._interval); | |
| } | |
| getTime(){ | |
| let t = new Date(); | |
| return `${t.getHours()}:${t.getMinutes()}:${t.getSeconds()}`; | |
| } | |
| render() { | |
| let {children: child} = this.props; | |
| return cloneElement(Children.only(child), {time: this.state.time}); | |
| } | |
| } | |
| class ShowCurrentTime extends Component { | |
| render() { | |
| return ( | |
| <div> | |
| <span style={{color: 'blue'}}>Time is {this.props.time}</span> | |
| </div> | |
| ); | |
| } | |
| } | |
| render( | |
| <div> | |
| <CurrentTime> | |
| <ShowCurrentTime /> | |
| </CurrentTime> | |
| </div>, document.getElementById('home') | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment