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

登录时遇到的报错问题,求解决方法 #11054

Closed
weeptao opened this issue Nov 23, 2023 · 1 comment
Closed

登录时遇到的报错问题,求解决方法 #11054

weeptao opened this issue Nov 23, 2023 · 1 comment

Comments

@weeptao
Copy link

weeptao commented Nov 23, 2023

image

我在登录页获取token,然后再调refresh方法拿用户信息。但是我先执行refresh后,他会立即刷新页面,从而走
if(initialState){ // 已经登陆了,返回首页 return <Navigate to="/" /> } 这个方法,然后又走了history.push('/'),然后就报错了。想问下有没有什么方法

@chenshuai2144
Copy link
Collaborator

根据你的描述,你在登录页获取了token,并在调用refresh方法来获取用户信息。但是在执行refresh之后立即刷新了页面,导致走了if(initialState)这个条件分支,然后又走了history.push('/'),最终报错了。

根据代码片段来看,if(initialState)这个条件分支是在判断用户是否已经登录,如果已经登录,则跳转到首页。

解决这个问题的方法是,确保在调用refresh方法之后不要立即刷新页面,而是等到获取到用户信息后再决定是否跳转到首页。你可以在调用refresh方法之后加上一个回调函数,等待refresh方法执行完毕后再进行下一步操作。

具体的代码修改如下:

refresh(() => {
  // 获取用户信息后的回调函数
  if(initialState){
    // 已经登陆了,返回首页
    return <Navigate to="/" />
  }
});

这样就能够避免在没有获取到用户信息之前就跳转页面,从而解决报错问题。

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