你好,我context在 React 中乱搞,我想知道是否有办法在同一个类中使用 2 个上下文static。我知道我可以用 2 个消费者来做这件事,并访问他们的价值,返回一个像文档所说的函数,但我想在整个类中访问这两个上下文,就像使用static contextType = HomeContext. 有没有办法做这样的事情?:
FormControl.contextType = {
HomeContext,
FormContext
}
Run Code Online (Sandbox Code Playgroud)
无法使用contextTypeApi访问多个上下文。相反,您需要使用 Render props 模式
<HomeContext.Consumer>
{(homeContext) => (
<FormContext.Consumer>
{(formContext) => (
<YourComponent homeContext={homeContext} formContext={formContext} />
)}
</FormContext.Consumer>
)}
</HomeContext.Consumer>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2478 次 |
| 最近记录: |