React学习案例十五

Posted hhh江月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了React学习案例十五相关的知识,希望对你有一定的参考价值。

React学习案例十五

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>React 实例</title>
</head>
<body>

<div id="example"></div>
<script type="text/babel">
function FormattedDate(props) 
  return <h2>现在是 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>
    );
  


function App() 
  return (
    <div>
      <Clock />
      <Clock />
      <Clock />
    </div>
  );


ReactDOM.render(<App />, document.getElementById('example'));
</script>

</body>
</html>



<!-- <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>React 实例</title>
</head>
<body>

<div id="example"></div>
<script type="text/babel">
function FormattedDate(props) 
  return <h2>现在是 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>
    );
  


function App() 
  return (
    <div>
      <Clock />
      <Clock />
      <Clock />
    </div>
  );


ReactDOM.render(<App />, document.getElementById('example'));
</script>

</body>
</html> -->


<body>

<div id="example"></div>
<script type="text/babel">
function FormattedDate(props) 
  return <h2>现在是 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
    );
  

ReactDOM.render(<App />, document.getElementById('example'));
</script>

</body>
</html> -->


<body>

<div id="example"></div>
<script type="text/babel">
function FormattedDate(props) 
  return <h2>现在是 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>
    );
  


function App() 
  return (
    <div>
      <Clock />
      <Clock />
      <Clock />
    </div>
  );


ReactDOM.render(<App />, document.getElementById('example'));
</script>

以上是关于React学习案例十五的主要内容,如果未能解决你的问题,请参考以下文章

React 入门学习(十五)-- React-Redux 基本使用

React 入门学习(十五)-- React-Redux 基本使用

React 入门学习(十五)-- React-Redux 基本使用

机器学习实战应用案例100篇(十五)-萤火虫算法从原理到实战应用案例

机器学习实战应用案例100篇(二十五)-强联通分量算法应用案例

第十五周学习进度