iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 6
0
Modern Web

30天React從入門到入坑系列 第 6

DAY6:state與資料流

props是從父元件到子元件,資料是單向流動。而state只關心元件內部,它是私有的參數只受元件控管。當元件內部使用setState方法時,元件會進行重新繪製。props與state的差異在於,props可從外部傳入而state封裝在元件裡只受元件控製。

src/SayHi.js

import React, { Component } from 'react';

class SayHi extends React.Component {
  constructor(props) {
    super(props);
    this.state = {date: new Date()};
  }

  render() {
    return <h1>{this.state.date.toLocaleTimeString()} Hi, {this.props.name}</h1>;
  }
}

export default SayHi;

輸出結果
https://ithelp.ithome.com.tw/upload/images/20171225/201073175znUhsvLL3.png

鐵人破病中,原本預計今天寫的lifecycle移到明天...


上一篇
DAY5:新朋友Components and Props
下一篇
DAY7:React Lifecycle
系列文
30天React從入門到入坑30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言