1
저는 반응과 플럭스의 초보자입니다. ReactRouter (2.4)를 사용할 때 다음과 같은 문제가 발생했습니다.플럭스 알림 후 ReactRouter가 올바르게 리디렉션하지 않습니다.
hashHistory를 사용하고 있는데 성공한 후에 "/ login"페이지에있을 때 "/"페이지로 리디렉션해야합니다. 로그인 시도.
라우터
ReactDOM.render(
<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRoute component={ErasmusPage} onEnter={authCheck}></IndexRoute>
<Route path="login"component={withRouter(LoginPage)}></Route>
</Route>
</Router>, app);
로그인 페이지
constructor() {
super();
this.notifyLogin = this.notifyLogin.bind(this);
this.state = {
email: "",
password: ""
};
}
componentWillMount() {
authStore.on("login", this.notifyLogin);
}
componentWillUnmount() {
authStore.removeListener("login", this.notifyLogin);
}
notifyLogin() {
this.props.router.push('/');
}
...
handleSubmit(e) {
e.preventDefault();
let data = {
email: this.state.email,
password: this.state.password
};
AuthActions.authenticate(data);
}
...
흐름은 다음과 같습니다
- 하나 (아약스 호출이 포함), 제출 authActions 및 저장이 datas 정교한 후 .
- 로그인 시도가 괜찮 으면 AuthStore가 "로그인"신호를 내 보냅니다 ...
- ... 그래서 notifyLogin()을 실행할 수 있습니다.
문제는 this.props.router.push ('/')가 제대로 리디렉션하지 않으면 URL이 변경되지만 페이지는 표시되지 않습니다 (상태 새로 고침이 트리거되지 않은 것으로 보입니다).
handleSubmit 함수에 this.props.router.push ('/')를 넣으면 이상한 일입니다. 리디렉션은 완벽하게 작동합니다.
무슨 일이 벌어지고 있는지 알고 싶습니다.
음, 예를 로그인하지 않은 경우, 렌더링 로그인 페이지 구성 요소를 렌더링, 그렇지 않으면, 렌더링에
음, 점점 더 비동기 콜백을 수행했다면 다음과 같이 시도해 보았습니다. setTimeout (() => { this.props.router.push ('/'); }, 1000) ; 작동하지만 해킹입니다 ... 누구의 아이디어가 더 좋습니까? 콜백에 파견하는 대신 축약의 약속을 상점에 전달하는 것이 잘못 되었습니까? –
아, ok @ user3549754 그럼에도 구성 요소가 로그인 동작을 트리거합니다. 로그인이 성공하면 해당 조치는 user_authenticated 조치를 전달하며, 이는 로그인 저장소에서이를 설정합니다. 이제 로그인 저장소에 새로운 소품이 제공됩니다. userAuthenticated prop가 true 인 경우 리디렉션되는 componentWillUpdate를 추가합니다. –