umijs / umi

A framework in react community ✨
https://umijs.org
MIT License
15.32k stars 2.65k forks source link

为什么 initialState.settings.title 会丢失? #11022

Closed hyzx86 closed 1 year ago

hyzx86 commented 1 year ago

我希望可以通过服务端设置站点名称 在 getInitalState 方法里 返回了settings 并根据服务端配置 修改了 settings 中的 主题配置 等 runtimeConfig 信息

AntdPro 代码示例 image

在 app.tsx 中如下位置 输出 initialState

// ProLayout 支持的api https://procomponents.ant.design/components/layout
export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) => {
  console.log('layout_initialState: ', initialState);
  return {
    //处理title 丢失问题
    title: initialState.settings.serverTitle,

输出了5次,最后一次输出 的时候 title属性没了~~ 请问这是什么原因?我在源码里面找也没找到怎么回事~~

image

最小复现: https://github.com/ant-design/ant-design-pro/compare/master...hyzx86:ant-design-pro:titleLost

github-actions[bot] commented 1 year ago

由于缺乏足够的信息,我们暂时关闭了该 Issue。请修改(不要回复) Issue 提供最小重现以重新开启。谢谢。

fz6m commented 1 year ago

给一个会丢失的最小复现看看。

hyzx86 commented 1 year ago

复现我在做。。但是编译不过去-- 在 antdPro 项目上做的

async function getSettings(): Promise<Partial<LayoutSettings>> {
  return {
    ...defaultSettings,
    title: '张三李四王麻子',
  }  as Partial<LayoutSettings>;
}

/**
 * @see  https://umijs.org/zh-CN/plugins/plugin-initial-state
 * */
export async function getInitialState(): Promise<{
  settings?: Partial<LayoutSettings>;
  currentUser?: API.CurrentUser;
  loading?: boolean;
  fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
}> {
  const fetchUserInfo = async () => {
    try {
      const msg = await queryCurrentUser({
        skipErrorHandler: true,
      });
      return msg.data;
    } catch (error) {
      history.push(loginPath);
    }
    return undefined;
  };
  // 如果不是登录页面,执行
  const { location } = history;
  if (location.pathname !== loginPath) {
    const currentUser = await fetchUserInfo();
    return {
      fetchUserInfo,
      currentUser,
      settings: await getSettings(),
    };
  }
  return {
    fetchUserInfo,
    settings: await getSettings(),
  };
}
hyzx86 commented 1 year ago

最小复现: https://github.com/ant-design/ant-design-pro/compare/master...hyzx86:ant-design-pro:titleLost

fz6m commented 1 year ago
// app.tsx

    childrenRender: (children) => {
      // if (initialState?.loading) return <PageLoading />;
      return (
        <>
          {children}
          {isDev && (
            <SettingDrawer
              disableUrlParams
              enableDarkTheme
              settings={initialState?.settings}
              onSettingChange={(settings) => {
                // 🟡 添加完 title 之后,settings 变了,触发了 on change 事件,接下文
                setInitialState((preInitialState) => ({
                  ...preInitialState,
                  settings,
                }));
              }}
            />
          )}
        </>
      );
    },

这里把 title 清了:https://github.com/ant-design/pro-components/blob/b141035a6c7bd6cf83561b5d32f8e078a1f5675d/packages/layout/src/components/SettingDrawer/index.tsx#L130

hyzx86 commented 1 year ago

感谢!