如何在类组件中定义多个静态contextType?

问题描述

你好,我想知道当我需要在一个类组件方法中使用多个上下文时,如何定义多个静态ContextType。

export default class Component extends React.Component{

static contextType = MyContext1; //and MyContext2

constructor(props){
    super(props);
}

componentDidMount = () => console.log(this.context); //need Context2

render(){
    return(
        <React.Fragment>
            {}
        </React.Fragment>
    );
}

我知道我可以通过与render方法中的标记相同的方式来做到这一点,但是我需要在其他方法中使用它,并且我想不使用标记就消耗上下文。

render() {
    <MyContext1.Consumer>
      {(props1) => (
        <MyContext2.Consumer>
          {(props2) => (
            <>....</>
          )}
        </MyContext2.Consumer>
      )}
    </MyContext1.Consumer>
  }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)