首页 > 其他 > 详细

React State(状态)

时间:2018-11-14 18:08:35      阅读:124      评论:0      收藏:0      [点我收藏+]
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‘))

 

React State(状态)

原文:https://www.cnblogs.com/shui1993/p/9959155.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!