加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react 中文文档案例一 (倒计时)

发布时间:2020-12-15 20:35:09 所属栏目:百科 来源:网络整理
导读:? 1.函数试组件 import React from ‘ react ‘ ;import ReactDOM from ‘ react-dom ‘ ; function Clock(props){ return ( div h1Hello,world!/h1 h2It is {props.date.toLocaleTimeString()}./h2 /div );}function tick() { ReactDOM.render( Clock date=

?

1.函数试组件

import React from react;
import ReactDOM from react-dom;

function Clock(props){
    return(
        <div>
            <h1>Hello,world!</h1>
            <h2>It is {props.date.toLocaleTimeString()}.</h2>
        </div>
    );
}

function tick() {
    ReactDOM.render(
        <Clock date={new Date()}/>,document.getElementById(root)
    );
}
  
setInterval(tick,1000);

2.函数试组件改成类组件

 
 
import React from ‘react‘;
import ReactDOM from ‘react-dom‘;
class Clock extends React.Component {
    render() {
      return (
        <div>
          <h1>Hello,world!</h1>
          <h2>It is {this.props.date.toLocaleTimeString()}.</h2>
        </div>
      );
    }
  }
function tick() {
    ReactDOM.render(
        <Clock date={new Date()}/>,document.getElementById(root)
    );
}
  
  setInterval(tick,1000);

?

import React from react;
import ReactDOM from react-dom;

function FormattedDate(props) {
    return <h2>It is {props.date.toLocaleTimeString()}.</h2>;
}
  
class Clock extends React.Component {
    constructor(props) {
      super(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>
          <h1>Hello,world!</h1>
          <FormattedDate date={this.state.date} />
        </div>
      );
    }
  }
  
class App extends React.Component {
    render(){
        return (
            <div>
              <Clock />
              <Clock />
              <Clock />
            </div>
          );
    }
}
ReactDOM.render(
    <App />,document.getElementById(root)
);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读