在 react-router 2.4.0 中重定向 willTransitionTo?
Redirecting willTransitionTo in react-router 2.4.0?
我目前在我的聊天应用程序上使用 "react-router": "^2.4.0"
,并且对如何在没有用户时重定向感到困惑。我认为 redirect 在 "^2.4.0"
中不起作用。
我应该在 聊天 路径上使用 onEnter
挂钩吗?
像这样:
<Route path="chat" component={Chat} onEnter={Chat.willTransitionTo}/>
routes.js
<Route path="/" component={App} >
<IndexRoute component={Chat} />
<Route path="chat" component={Chat} />
<Route path="login" component={Login} />
</Route>
Chat.jsx
static willTransitionTo(transition){
var state = ChatStore.getState();
if(!state.user){
transition.redirect('/login');
}
}
至少有一个拼写错误:
tansition.redirect('/login');
应该是:
transition.redirect('/login');
您还应该查看 react-router 的 auth-flow 示例:https://github.com/reactjs/react-router/tree/master/examples/auth-flow
我通过使用 setRouteLeaveHook
使其工作,如下所示:
https://github.com/ReactTraining/react-router/blob/master/docs/guides/ConfirmingNavigation.md
return false to prevent a transition w/o prompting the user
但是,他们忘记提到钩子也应该取消注册,参见 here and here。
我们可以使用它来实现我们自己的解决方案,如下所示:
class YourComponent extends Component {
constructor() {
super();
const {route} = this.props;
const {router} = this.context;
this.unregisterLeaveHook = router.setRouteLeaveHook(
route,
this.routerWillLeave.bind(this)
);
}
componentWillUnmount() {
this.unregisterLeaveHook();
}
routerWillLeave() {
// return false to prevent a transition w/o prompting the user,
// or return a string to allow the user to decide:
if (!this.state.isSaved) {
return 'Your work is not saved! Are you sure you want to leave?'
}
}
...
}
YourComponent.contextTypes = {
router: routerShape
};
我目前在我的聊天应用程序上使用 "react-router": "^2.4.0"
,并且对如何在没有用户时重定向感到困惑。我认为 redirect 在 "^2.4.0"
中不起作用。
我应该在 聊天 路径上使用 onEnter
挂钩吗?
像这样:
<Route path="chat" component={Chat} onEnter={Chat.willTransitionTo}/>
routes.js
<Route path="/" component={App} >
<IndexRoute component={Chat} />
<Route path="chat" component={Chat} />
<Route path="login" component={Login} />
</Route>
Chat.jsx
static willTransitionTo(transition){
var state = ChatStore.getState();
if(!state.user){
transition.redirect('/login');
}
}
至少有一个拼写错误:
tansition.redirect('/login');
应该是:
transition.redirect('/login');
您还应该查看 react-router 的 auth-flow 示例:https://github.com/reactjs/react-router/tree/master/examples/auth-flow
我通过使用 setRouteLeaveHook
使其工作,如下所示:
https://github.com/ReactTraining/react-router/blob/master/docs/guides/ConfirmingNavigation.md
return false to prevent a transition w/o prompting the user
但是,他们忘记提到钩子也应该取消注册,参见 here and here。
我们可以使用它来实现我们自己的解决方案,如下所示:
class YourComponent extends Component {
constructor() {
super();
const {route} = this.props;
const {router} = this.context;
this.unregisterLeaveHook = router.setRouteLeaveHook(
route,
this.routerWillLeave.bind(this)
);
}
componentWillUnmount() {
this.unregisterLeaveHook();
}
routerWillLeave() {
// return false to prevent a transition w/o prompting the user,
// or return a string to allow the user to decide:
if (!this.state.isSaved) {
return 'Your work is not saved! Are you sure you want to leave?'
}
}
...
}
YourComponent.contextTypes = {
router: routerShape
};