【发布时间】:2020-05-05 04:39:30
【问题描述】:
学习 React 并想知道以下使用上下文的方式在幕后是如何工作的?
static contextType = MyContext;
render() {
let value = this.context;
/* render something based on the value */
}
}
this.context 如何通过声明static contextType 自动设置?
【问题讨论】:
-
如果你在一个类上指定一个静态属性,比如
App,react 可以使用App.contextType访问该属性并将上下文值作为道具提供
标签: reactjs react-context