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, );

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, );
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(), );
} 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')
);

react 中文文档案例一 (倒计时)的更多相关文章

  1. react 中文文档案例三 (开关按钮)

    开关按钮制作   import React from 'react'; import ReactDOM from 'react-dom'; class Toggle extends React.Com ...

  2. react 中文文档案例七 (温度计)

    const scaleNames = { c: 'Celsius', f: 'Fahrenheit' }; function toCelsius(fahrenheit) { ) * / ; } fun ...

  3. react 中文文档案例六 (表单)

    class Reservation extends React.Component { constructor(props) { super(props); this.state = { isGoin ...

  4. react 中文文档案例五 (循环列表)

    function NumberList(props) { const numbers = props.numbers; const listItems = numbers.map((number) = ...

  5. react 中文文档案例四 (登陆登出按钮)

    import React from 'react'; import ReactDOM from 'react-dom'; class LoginControl extends React.Compon ...

  6. react 中文文档案例二 (头像时间)

    import React from 'react'; import ReactDOM from 'react-dom'; function formatDate(date) { return date ...

  7. React中state与props介绍与比较

    一.state 1.state的作用 state是React中组件的一个对象.React把用户界面当做是状态机,想象它有不同的状态然后渲染这些状态,可以轻松让用户界面与数据保持一致. React中,更 ...

  8. React.js 中文文档

    转自http://react-china.org/t/react-js/398的jsgeeker 中文文档地址 http://reactjs.cn GitHub地址 https://github.co ...

  9. react中简单倒计时跳转

    其实在react中实现倒计时的跳转方法有很多中,其中我认为较为好用的就是通过定时器更改state中的时间值. 首先在constructor中设置10秒的时间值: constructor () { su ...

随机推荐

  1. RPC: program not registered (ZT)

    When we trying to use a particular RPC  program, below may indicate that rpcbind is not running or t ...

  2. C#改变LInqToSQL的引用地址,读取config的数据库字符串

    C#改变LInqToSQL的引用地址,读取config的数据库字符串修改Properties 下 Settings.Settings 下 Settings.Designer.cs 下 return ( ...

  3. Unknown error: Unable to build: the file dx.jar was not loaded from the SDK folder!

    Eclipse 编译没问题,但是运行就出现这个报错:Unknown error: Unable to build: the file dx.jar was not loaded from the SD ...

  4. App.CSharp.Grid的ICells接口

    using System;using System.Collections.Generic;using System.Text;using System.Drawing;using System.Wi ...

  5. 根据/proc/meminfo对空闲内存进行占用

    #include <stdio.h> #include <sys/sysinfo.h> #include <linux/kernel.h> /* 包含sysinfo ...

  6. socket多线程方式案例

    记下来,方便以后查看 User类 package com.xujingyang.ThreadSocket; import java.io.Serializable; public class User ...

  7. docker创建容器打开两个端口

     docker run -d -it --name c6_3 -v :/mnt -p 5000:8000 -p 3000  centos  注释: -v 后面为共享文件夹

  8. java中静态方法和非静态方法调用的一点小困扰,已解决。

    public static void main(String[] args) { // TODO Auto-generated method stub SimpleGui1B gui=new Simp ...

  9. vue 之 面向对象

    JavaScript 语言中,生成实例对象的传统方法是通过构造函数. function Animal(name,age){ this.name = name; this.age = age; } An ...

  10. jqgrid列动态加载

    private void InitGrid(string entityName) { Session["entityName"] = entityName; ArrayList c ...