国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

react.js 父子組件數(shù)據(jù)綁定實(shí)時(shí)通訊的示例代碼

2019-11-19 15:18:46
字體:
供稿:網(wǎng)友

react.js我自己還在摸索學(xué)習(xí)中,碰到父子組件數(shù)據(jù)綁定實(shí)時(shí)通訊的問題,研究了一下,分享給大家,也給自己留個(gè)筆記:

import React,{Component} from 'react'import ReactDOM from 'react-dom'class ChildCounter extends Component{  render(){    return(      <div style={{border:'1px solid red'}}>        {this.props.count}      </div>    )  }}/** 大家默認(rèn)規(guī)定的一些步驟,方便大家看* 1.默認(rèn)值* 2.初始化狀態(tài)* 3.鉤子函數(shù)* 4.方法函數(shù)* */class Counter extends Component{  //默認(rèn)屬性對象  static defaultProps={    number:5  }  constructor(props){    super(props);    //獲取我的初始狀態(tài)    this.state={      number:props.number    }  }  //鉤子函數(shù)  componentWillMount(){    console.log('組件將要掛載')  }  componentDidMount(){    console.log("組件掛載完成")  }  handleClick=()=>{    //this.setState方法是異步的,一個(gè)函數(shù)里面只能調(diào)用一次this.setState方法    //調(diào)用多次會合并,只執(zhí)行一次    this.setState((prev,next)=>({      //上一次的狀態(tài)prev      number:prev.number+1    }),()=>{      console.log("回調(diào)函數(shù)執(zhí)行")    })    // this.setState({index:this.state.index+1})  }  render(){    //調(diào)用子組件ChildCounter,把當(dāng)前狀態(tài)值傳過去    return(      <div>        <p>{this.state.number}</p>        <button onClick={this.handleClick}>+</button>        <ChildCounter count={this.state.number}></ChildCounter>      </div>    )  }}//渲染到頁面ReactDOM.render(<Counter></Counter>,document.querySelector("#root"))

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 丹巴县| 克山县| 新蔡县| 潍坊市| 嵩明县| 荣成市| 耿马| 普格县| 涡阳县| 安陆市| 盐津县| 胶南市| 罗甸县| 永登县| 平顺县| 昌江| 盐池县| 辰溪县| 伊金霍洛旗| 西贡区| 江津市| 宁阳县| 洪湖市| 贺兰县| 商南县| 平利县| 河北省| 葫芦岛市| 开江县| 毕节市| 得荣县| 五大连池市| 合水县| 保亭| 新建县| 林口县| 花莲县| 上饶市| 蒙山县| 梅河口市| 鄱阳县|