亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React在父組件中如何拿到子組件的虛擬dom

React在父組件中如何拿到子組件的虛擬dom

云景前端 2016-01-24 01:03:02
目前我的方法是,用一個全局變量將子組件render時的虛擬dom實例保存下來,以方便在父組件中調用。那么問題就是如何不用變量直接在父組件中直接拿到子組件的虛擬dom呢?//?全局變量,用于記錄每個子組件的虛擬DOM實例 ????var?childrenComponent=[]; ???? ????//?子組件 ????var?Child?=?React.CreateClass({ ????????componentDidMount:?function?(){ ????????????//?將組件最后的react實例保存下來,方便在父組件中調用其屬性和方法。 ? ????childrenComponent.push(this); ? }, ????????render:function?(){ ????????????return( ????????????????<p>this?is?a?child?component,number:{this.props.index}</p> ????????????) ????????} ????}) ???? ????//父組件 ????var?Parent?=?React.CreateClass({ ????????handleCilck:function?(){ ????????????//這里拿到子組件的虛擬DOM,以進行一些操作 ????????????console.log(childrenComponent[0]) ????????}, ????????render:function?(){ ????????????var?children?=?[]; ????????????var?newChild?=?function?(i){ ????????????????return?<Child?index={i}?/>? ????????????}? ????????????for(var?i=0,?i<5,?i++){ ????????????????children.push(newChild(i)) ????????????} ????????????return( ????????????????<div> ????????????????????{children} ????????????????????<button?onClick={this.handleClick}></button> ????????????????</div> ????????????) ????????} ????}) ???? ????React.render( ????<Parent?/>, ????document.body );
查看完整描述

2 回答

?
我們沒有朋友

TA貢獻1條經驗 獲得超0個贊

你這個方法最實用了! 感謝分享

查看完整回答
反對 回復 2016-07-21
  • 2 回答
  • 1 關注
  • 4530 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號