Javascript 如何在类组件中定义多个静态contextType?
您好,我想知道当我需要在一个类组件方法中使用多个上下文时,如何定义多个静态ContextTypeJavascript 如何在类组件中定义多个静态contextType?,javascript,reactjs,react-native,context-api,Javascript,Reactjs,React Native,Context Api,您好,我想知道当我需要在一个类组件方法中使用多个上下文时,如何定义多个静态ContextType export default class Component extends React.Component{ static contextType = MyContext1; //and MyContext2 constructor(props){ super(props); } componentDidMount = () => console.log(this.contex
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>
);
}
导出默认类组件扩展React.Component{
静态contextType=MyContext1;//和MyContext2
建造师(道具){
超级(道具);
}
componentDidMount=()=>console.log(this.context);//需要Context2
render(){
返回(
{}
);
}
我知道我可以通过标签来完成它,就像render方法中的follow一样,但我在其他方法中需要它,并且我希望使用上下文而不使用标签
render() {
<MyContext1.Consumer>
{(props1) => (
<MyContext2.Consumer>
{(props2) => (
<>....</>
)}
</MyContext2.Consumer>
)}
</MyContext1.Consumer>
}
render(){
{(props1)=>(
{(props2)=>(
....
)}
)}
}