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

👑 [需求 | Feature] 初次进页面无法拿到 initialState,调用下refresh(),让登录成功不跳转说拜拜 #10907

Closed
codedart2018 opened this issue Aug 24, 2023 · 1 comment
Labels

Comments

@codedart2018
Copy link

codedart2018 commented Aug 24, 2023

🥰 需求描述 | Requirements description

本地开发的时候没有注意,因为当时地址上都带有路由或参数,但给客户的地址是一个干净地址,进去页面客户点击登录提示登录成功,但就是进不了后台。后来试了下确实也是不进入后台,然后在登录的时候打印了下initialState对象,发现是一个undefined,它当然无法调用fetchUserInfo无法跳转登录喽!

🧐 解决方案 | Solution

在登录页面useEffect里加个刷新,让它进入页面去调用下刷新initialState,让它不在是一个undefined

 const { initialState, refresh, setInitialState } = useModel('@@initialState');
  useEffect(() => {
    refresh();
  }, []);

🚑 其他信息 | Other information

如果还不能解决你问题的话,直接暴力写个方法调用,不去调用ininitalState.fetchUserInfo.


  const fetchUserInfo = async () => {
    //如果登录时候initialState 还是一个undefined 直接写一个方法调用代替它。
    const userInfo = await initialState?.fetchUserInfo?.();
    if (userInfo) {
      flushSync(() => {
        setInitialState((s) => ({
          ...s,
          currentUser: userInfo,
        }));
      });
    }
  };
@echoyl
Copy link

echoyl commented Aug 26, 2023

登录后回调中直接
flushSync(() => { setInitialState((s) => ({ ...s, currentUser: res.data.userinfo })).then(() => { const redirect = searchParams.get('redirect') || '/'; history.push(redirect); }); });

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

No branches or pull requests

3 participants