对于每个加载的组件,我需要检查cookie是否存在,但是在呈现this.requireLogin
函数时出现此错误:
正在加载组件未定义。
class App extends Component {
requireLogin = (component) => {
// checking cookie Exists
if(!cookieExist){
axios.get("api/login")
.then(res => window.location.assign(res.data))
.catch(err => console.log("error occured on login service", err))
return <div><Loading /></div>
}else{
return <${component} />
}
}
render() {
return(
<BrowserRouter>
<Header />
<div className="content">
<Route path="/aboutus" render={() => this.requireLogin("AboutUs")} />
<Route path="/contact" render={() => this.requireLogin("Contact")/>
<Route path="/" render={() => this.requireLogin("HomePage")}
</div>
<Footer />
</BrowserRouter>
)
}
}