function FormattedDate(props){
  return (
    <h1>现在是{props.date}</h1>
  )
}
class Clock extends React.Component{
  constructor(props){
    supper(props);
    this.state={date:new Date()};
  }
  componentDidMount(){
    this.timerId=setInterval(()=>this.tick(),1000);
  }
  componentWillUnmount(){
    clearInterval(this.timerId)
  }
  tick(){
    this.setState({
      date:new Date()
    })
  }
  render(){
    return{
      <div>
        <FormattedDate date={this.state.date}/>
      </div>
    }
  }
}
function App(){
  return{
    <div>
      <Clock/>
      <Clock/>
    </div>
  }  
}
ReactDOm.render(<App/>,document.getElementById('example'))

 

posted on 2018-11-14 17:29  沉默的末1993  阅读(91)  评论(0编辑  收藏  举报