1
1つのパスに対して2つのコンポーネントを持つ1つのアプリケーションを作成しています。私はAppContainer
コンポーネントを使用して私のアプリの中に入っています。この場合authの場合は、リアクタルータの複数コンポーネント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>
:ここに私のルートです。その代わりに、私はpath="/"
を持つホームページを持っているので、そのコンポーネントは私の選択でなければなりません。
function requireEnter (nextState, replace) {
if (nextState.location.pathname == "/") {
if (checkLoggedIn()) {
replace({
pathname: '/home'
})
} else {
replace({
pathname: '/homepage'
})
}
}
}
しかし、私はこのような何かことをしたい:今
onEnter={requireEnter}
は、次の場合に処理しているあなたはそれでいないコンポーネントにルートを追加することができます
function requireEnter (nextState, replace) {
if (nextState.location.pathname == "/") {
if (checkLoggedIn()) {
//component should be AppContainer and redirect to '/home'
} else {
//component should be home page
}
}
}