1
我正在創建一個應用程序,其中包含2個組件的單個路徑。這裏是我的路線:在auth的情況下,React-router多組件onEnter?
<Router history={hashHistory}>
<Route path="/" component={AppContainer} onEnter={requireEnter}>
<Route path="/homepage" component={HomePage} />
<Route component={MainPage} onEnter={requireAuth}>
<Route path="/home" component={DashBoard} />
</Route>
</Route>
</Router>
在這種情況下,我使用的是AppContainer
成分進入我的應用程序。而不是我希望那個組件應該是我的選擇,因爲我有一個主頁,將有path="/"
。
眼下onEnter={requireEnter}
正在處理包括以下情況:
function requireEnter (nextState, replace) {
if (nextState.location.pathname == "/") {
if (checkLoggedIn()) {
replace({
pathname: '/home'
})
} else {
replace({
pathname: '/homepage'
})
}
}
}
但我想這樣的事情:
function requireEnter (nextState, replace) {
if (nextState.location.pathname == "/") {
if (checkLoggedIn()) {
//component should be AppContainer and redirect to '/home'
} else {
//component should be home page
}
}
}