反应树中未定义的上下文

时间:2020-07-17 07:04:12

标签: javascript reactjs

我到处都收到不确定的静态上下文,我错过了什么?路由器能否以某种方式影响它,或者我只是忘记了一些小部分。这个想法是定义用户浏览器,并在我所有组件中分配结果是非。谢谢

I am: eating
I am: eating a pine apple

export const GlobalContext = React.createContext(false);

    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;

1 个答案:

答案 0 :(得分:0)

您必须使用contextconsumer来使用上下文值

请在以下链接中找到上下文API的参考:

https://reactjs.org/docs/context.html#contextconsumer