Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

登录成功后跳转首页的逻辑是什么 #17

Open
RichardZhang12 opened this issue Apr 28, 2021 · 1 comment
Open

登录成功后跳转首页的逻辑是什么 #17

RichardZhang12 opened this issue Apr 28, 2021 · 1 comment

Comments

@RichardZhang12
Copy link

你好,我碰见一些情况,请教一下。

问题:研究代码,login页面,点击登录按钮,先获取token-->获取用户信息,但是获取成功之后没有看到进行路由跳转的代码。我本地调试看到了点击登录按钮成功后,login组件进行了重新渲染,token存在,所以返回,但是为什么点击登录后,login组件会重新渲染呢,

@drinkjava2
Copy link

drinkjava2 commented Jun 17, 2022

我正好也在学习这个,多打些断点就明白了,简单记录一下我的分析:
登录按钮后依次调用:
handleSubmit
-> handleLogin(username, password)
-> login(username, password)
-> dispatch(setUserToken(token));
dispach了一个types.USER_SET_USER_TOKEN类型的action, 造成reducer里的对应action执行了,结果就是state里的user变量被改变了,react就从最顶层重新开始刷新,因为当前页面还是/login,所以继续执行
这个路由,然后还是在Login函数里碰到

if (token) {
    return <Redirect to="/dashboard" />;
 }

这时因为user.token有值了,所以会跳转到/dashboard路由上,重新刷新就跑到 <Route path="/" 。。。>这个路由里去了,也就是去加载Layout了, 至此登录过程完成。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants