Переглянути джерело

style: fix setInitialState async function (#8732)

陈帅 4 роки тому
батько
коміт
e211eac73b

+ 1 - 1
package.json

@@ -88,7 +88,7 @@
     "@umijs/plugin-openapi": "^1.2.0",
     "@umijs/plugin-openapi": "^1.2.0",
     "@umijs/preset-ant-design-pro": "^1.2.0",
     "@umijs/preset-ant-design-pro": "^1.2.0",
     "@umijs/preset-dumi": "^1.1.7",
     "@umijs/preset-dumi": "^1.1.7",
-    "@umijs/preset-react": "^1.7.4",
+    "@umijs/preset-react": "^1.8.17",
     "@umijs/yorkie": "^2.0.3",
     "@umijs/yorkie": "^2.0.3",
     "carlo": "^0.9.46",
     "carlo": "^0.9.46",
     "cross-env": "^7.0.0",
     "cross-env": "^7.0.0",

+ 3 - 3
src/components/RightContent/AvatarDropdown.tsx

@@ -36,14 +36,14 @@ const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({ menu }) => {
   const onMenuClick = useCallback(
   const onMenuClick = useCallback(
     (event: MenuInfo) => {
     (event: MenuInfo) => {
       const { key } = event;
       const { key } = event;
-      if (key === 'logout' && initialState) {
-        setInitialState({ ...initialState, currentUser: undefined });
+      if (key === 'logout') {
+        setInitialState((s) => ({ ...s, currentUser: undefined }));
         loginOut();
         loginOut();
         return;
         return;
       }
       }
       history.push(`/account/${key}`);
       history.push(`/account/${key}`);
     },
     },
-    [initialState, setInitialState],
+    [setInitialState],
   );
   );
 
 
   const loading = (
   const loading = (

+ 8 - 14
src/pages/user/Login/index.tsx

@@ -29,16 +29,6 @@ const LoginMessage: React.FC<{
   />
   />
 );
 );
 
 
-/** 此方法会跳转到 redirect 参数所在的位置 */
-const goto = () => {
-  if (!history) return;
-  setTimeout(() => {
-    const { query } = history.location;
-    const { redirect } = query as { redirect: string };
-    history.push(redirect || '/');
-  }, 10);
-};
-
 const Login: React.FC = () => {
 const Login: React.FC = () => {
   const [submitting, setSubmitting] = useState(false);
   const [submitting, setSubmitting] = useState(false);
   const [userLoginState, setUserLoginState] = useState<API.LoginResult>({});
   const [userLoginState, setUserLoginState] = useState<API.LoginResult>({});
@@ -50,10 +40,10 @@ const Login: React.FC = () => {
   const fetchUserInfo = async () => {
   const fetchUserInfo = async () => {
     const userInfo = await initialState?.fetchUserInfo?.();
     const userInfo = await initialState?.fetchUserInfo?.();
     if (userInfo) {
     if (userInfo) {
-      setInitialState({
-        ...initialState,
+      await setInitialState((s) => ({
+        ...s,
         currentUser: userInfo,
         currentUser: userInfo,
-      });
+      }));
     }
     }
   };
   };
 
 
@@ -69,7 +59,11 @@ const Login: React.FC = () => {
         });
         });
         message.success(defaultloginSuccessMessage);
         message.success(defaultloginSuccessMessage);
         await fetchUserInfo();
         await fetchUserInfo();
-        goto();
+        /** 此方法会跳转到 redirect 参数所在的位置 */
+        if (!history) return;
+        const { query } = history.location;
+        const { redirect } = query as { redirect: string };
+        history.push(redirect || '/');
         return;
         return;
       }
       }
       // 如果失败去设置用户错误信息
       // 如果失败去设置用户错误信息