5
react-router-2を使用しています。ログインに成功した後、または何らかのアクションが実行された後、プログラムでページにリダイレクトしたい。反応ルータのプログラムでページにリダイレクトする2
マイルート・ファイルには、私が後ログインcomponent
からダッシュボードcomponent
にリダイレクトしたい、この(routes.js)フック
function redirectToLogin(nextState, replace) {
// Perform some authentication check
if (!loggedIn) {
replace({
pathname: '/login',
state: { nextPathname: nextState.location.pathname }
});
}
}
function redirectToDashboard(nextState, replace) {
// Perform some check if already authenticated
if (loggedIn) {
replace('/dashboard')
}
}
フォーカス取得時
<Route path="/" component={App}>
<IndexRoute component={Home}/>
<Route path="/login" component={Login} onEnter={redirectToDashboard}/>
<Route path="dashboard" component={Dashboard} onEnter={redirectToLogin}/>
</Route>
のようなものですログインに成功しました。