這篇文章給大家分享的是有關(guān)react.js 父子組件數(shù)據(jù)如何綁定實(shí)時(shí)通訊的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
成都創(chuàng)新互聯(lián)公司始終堅(jiān)持【策劃先行,效果至上】的經(jīng)營(yíng)理念,通過(guò)多達(dá)10余年累計(jì)超上千家客戶的網(wǎng)站建設(shè)總結(jié)了一套系統(tǒng)有效的營(yíng)銷解決方案,現(xiàn)已廣泛運(yùn)用于各行各業(yè)的客戶,其中包括:木制涼亭等企業(yè),備受客戶贊譽(yù)。
示例:
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)屬性對(duì)象 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)用多次會(huì)合并,只執(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)值傳過(guò)去 return( <div> <p>{this.state.number}</p> <button onClick={this.handleClick}>+</button> <ChildCounter count={this.state.number}></ChildCounter> </div> ) } } //渲染到頁(yè)面 ReactDOM.render(<Counter></Counter>,document.querySelector("#root"))
感謝各位的閱讀!關(guān)于“react.js 父子組件數(shù)據(jù)如何綁定實(shí)時(shí)通訊”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
本文標(biāo)題:react.js父子組件數(shù)據(jù)如何綁定實(shí)時(shí)通訊
本文路徑:http://chinadenli.net/article14/ppsdge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、外貿(mào)建站、虛擬主機(jī)、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)