[英]Undefined context in the react tree
我到处都收到未定义的 static 上下文,我错过了什么? 路由器会以某种方式影响它,或者我只是忘记了一些小部分。 这个想法是定义用户浏览器并在我的所有组件中分配结果真/假。 谢谢
class App extends Component {
static contextType = GlobalContext;
isBrowserChrome = () => {
return window.navigator.userAgent.indexOf('Chrome') !== -1;
};
render() {
return (
<GlobalProvider isChrome={this.isBrowserChrome()} >
<AppLayout>
<PageLayout>
<Loading/>
<PureModal/>
<ContentLayout>
<DashboardLayout>
{this.props.auth.authenticated && <EpMenu/> }
<div style={{marginTop: '20px'}}>
<Switch>
<Route exact path="/login/:token?" component={LoginPage}/>
<PrivateRoute exact path="/welcome" component={Welcome}/>
</Switch>
</div>
</DashboardLayout>
</ContentLayout>
</PageLayout>
</AppLayout>
</GlobalProvider>
);
}
}
App.contextType = GlobalContext;
export default App;
导出 const GlobalContext = React.createContext(false);
export class GlobalProvider extends Component {
render() {
return (
<GlobalContext.Provider value={this.props.isChrome}>
{this.props.children}
</GlobalContext.Provider>
)
}
}
您必须使用contextconsumer
才能使用上下文值
请在下面的链接中找到上下文 API 的参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.