admin管理员组文章数量:1323330
I am using react-router-2. I want to redirect to a page programmatically after successful login or after some action performed.
My route file is like this(routes.js)
<Route path="/" ponent={App}>
<IndexRoute ponent={Home}/>
<Route path="/login" ponent={Login} onEnter={redirectToDashboard}/>
<Route path="dashboard" ponent={Dashboard} onEnter={redirectToLogin}/>
</Route>
onEnter hooks
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')
}
}
I want to redirect to Dashboard ponent
from Login ponent
after successful login.
I am using react-router-2. I want to redirect to a page programmatically after successful login or after some action performed.
My route file is like this(routes.js)
<Route path="/" ponent={App}>
<IndexRoute ponent={Home}/>
<Route path="/login" ponent={Login} onEnter={redirectToDashboard}/>
<Route path="dashboard" ponent={Dashboard} onEnter={redirectToLogin}/>
</Route>
onEnter hooks
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')
}
}
I want to redirect to Dashboard ponent
from Login ponent
after successful login.
1 Answer
Reset to default 5To redirect you can use router
object from context. You have to declare context types in your ponent (ponent from which you make redirection). More about context link
ES6/7 syntax:
static contextTypes = {
router: React.PropTypes.object.isRequired
}
Now you have access to router object and you can make redirection:
this.context.router.push('/dashboard');
本文标签: javascriptredirect to a page programmatically in reactrouter 2Stack Overflow
版权声明:本文标题:javascript - redirect to a page programmatically in react-router 2 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742139510a2422518.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论