class B extends React.Component {
static contextType = myContext;
constructor(props) {
super(props)
}
render(){
const store=this.context
console.log(store.change1);
return <p style={{color:store.color}} onClick={store.change1}>titel</p>
}
}
设置static contextType = myContext,这比使用consumer好像简单一点,生命周期里也能拿到,不需要在嵌套一层,
如果B组件有两个consumer,这样好像就不行(只能声明一个context),如果单一consumer,是不是就可以不使用consumer,
使用consumer的优势是不是灵活一点,可以嵌套需要消费的代码块:
render(){
return
<div><p onClik={this.onClick}>titel</p>
<myConsumer>
{...}
<myConsumer/>
</div>
}
能否这样理解.