在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ React,父組件設(shè)置的context默認(rèn)值,如何在父組件使用?

React,父組件設(shè)置的context默認(rèn)值,如何在父組件使用?

下面這種試過不行!

//index.js

constructor(props, context) {
    super(props, context)
    this.context = {
        modelname: 'apiConfigurationModel',
    }
}

getChildContext() {
    return this.context
}

為了在父級組件使用和子組件同一個(gè)context

回答
編輯回答
愛礙唉

this.context是用來讀取這個(gè)component的父級components傳下來的context值啊,你這樣寫會出bug的。。。
加默認(rèn)值嘛,何必執(zhí)著于一定要放在context里。。。
比如可以:

const modelname = 'apiConfigurationModel';
class comp extends React.Component {
  getChildContext() {
    return {
      modelname
    };
  }
}

再如:

class comp extends React.Component {
  static modelname = 'apiConfigurationModel';
  getChildContext() {
    return {
      modelname: comp.modelname
    };
  }
}
2018年5月4日 20:52