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

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

react 高階組件 怎么獲取子組件的state?

react 高階組件 怎么獲取子組件的state?

開心每一天1111 2019-05-13 11:04:36
http://www.css88.com/react/do...看完官網文檔,大概知道高階組件怎么封裝了。大概知道高階組件怎么封裝了。當前的需求是怎么獲取WrappedPage的state?(我們是混合開發,需要更新header和右上角的圖標!希望在統一的地方處理header和右上角功能;而不是每個頁面都去添加重復代碼。)//basicimportReactfrom'react';importPropTypesfrom'prop-types';//modulesimportupfrom'../modules/cup';//WrappedPage每一個jsx路由頁面exportdefaultfunctionmixinsPage(WrappedPage){//……返回另一個新組件……returnclassPageextendsReact.Component{constructor(props){super(props);}componentWillMount(){console.log(WrappedPage);//清除頁面右上角的圖標up.plugins.setRightButton();}componentDidMount(){console.log(`created=>${this.state.badTitle}`);//設置titledocument.title=this.state.badTitle;up.plugins.setPageTitle(this.state.badTitle||'');//ios初始化''時無效}render(){//……使用最新的數據渲染組件//注意此處將已有的props屬性傳遞給原組件return;}}}采用反向繼承模式:可以操作state但是會導致WrappedPage的componentWillMount和componentWillMount不執行了。。。。exportdefaultfunctionmixinsPage(WrappedPage){//……返回另一個新組件……returnclassPageextendsWrappedPage{constructor(props){super(props);}componentWillMount(){//清除頁面右上角的圖標up.plugins.setRightButton();}componentDidMount(){//設置titledocument.title=this.state.badTitle;up.plugins.setPageTitle(this.state.badTitle||'');//ios初始化''時無效}shouldComponentUpdate(nextProps,nextState){console.log(nextState);}render(){//……使用最新的數據渲染組件//注意此處將已有的props屬性傳遞給原組件//return;//使用反向繼承模式:可以操作statereturnsuper.render()}}}以前vueJs的統一處理邏輯!//vueJs的mixinsbeforeCreate(){//清除頁面右上角的圖標plugins.setRightButton();},created(){//log.clearLogs();console.log(`created=>${this.badTitle}`);//設置titledocument.title=this.badTitle;plugins.setPageTitle(this.badTitle||'');//ios初始化''時無效},
查看完整描述

2 回答

?
ABOUTYOU

TA貢獻1812條經驗 獲得超5個贊

謝邀,使用ref即可。不懂可以翻閱文檔。
//basic
importReactfrom'react';
importPropTypesfrom'prop-types';
//modules
importupfrom'../modules/cup';
//WrappedPage每一個jsx路由頁面
exportdefaultfunctionmixinsPage(WrappedPage){
//……返回另一個新組件……
returnclassPageextendsReact.Component{
constructor(props){
super(props);
+this.WrappedPageRef=React.createRef()
}
componentWillMount(){
console.log(WrappedPage);
//清除頁面右上角的圖標
up.plugins.setRightButton();
}
componentDidMount(){
-console.log(`created=>${this.state.badTitle}`);
//設置title
-document.title=this.state.badTitle;
-up.plugins.setPageTitle(this.state.badTitle||'');//ios初始化''時無效
+if(this.WrappedPageRef.current){
+const{badTitle}=this.WrappedPageRef.current.state
+console.log(`created=>${badTitle}`);
+document.title=badTitle;
+up.plugins.setPageTitle(badTitle||'');//ios初始化''時無效
+}
}
render(){
//……使用最新的數據渲染組件
//注意此處將已有的props屬性傳遞給原組件
-return;
+return;
}
}
}
                            
查看完整回答
反對 回復 2019-05-13
?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

考慮下ref,this.refs.wrappedPage.state這個就是WrappedPage的state
//basic
importReactfrom'react';
importPropTypesfrom'prop-types';
//modules
importupfrom'../modules/cup';
//WrappedPage每一個jsx路由頁面
exportdefaultfunctionmixinsPage(WrappedPage){
//……返回另一個新組件……
returnclassPageextendsReact.Component{
constructor(props){
super(props);
}
componentWillMount(){
console.log(WrappedPage);
//清除頁面右上角的圖標
up.plugins.setRightButton();
}
componentDidMount(){
console.log(`created=>${this.state.badTitle}`);
//設置title
document.title=this.state.badTitle;
up.plugins.setPageTitle(this.state.badTitle||'');//ios初始化''時無效
}
render(){
//……使用最新的數據渲染組件
//注意此處將已有的props屬性傳遞給原組件
return;
}
}
}
                            
查看完整回答
反對 回復 2019-05-13
  • 2 回答
  • 0 關注
  • 2094 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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