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

fix: antd config setter conflict with model plugin #12406

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/plugins/src/antd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,19 @@ export default (api: IApi) => {
const hasConfigProvider = configProvider || enableV5ThemeAlgorithm;
// 拥有 `ConfigProvider` 时,我们默认提供修改 antd 全局配置的便捷方法(仅限 antd 5)
const antdConfigSetter = isV5 && hasConfigProvider;

// We ensure the `theme` config always exists to preserve the theme context.
// 1. if we do not config the antd `theme`, no theme react context is added.
// 2. when using the antd setter to change the theme, the theme react context will be added,
// then antd components re-render.
// 3. affected by the theme react context change, the model data context renders,
// gets the initial data again (a hack), and calls `setState`
// there is a conflict between 2 and 3, react does not allow `setState` calls during rendering.
// See https://github.com/umijs/umi/issues/12394
// https://github.com/ant-design/ant-design/blob/253b45eceb2c7760e8173ee0cd1003aecc00ef9c/components/config-provider/index.tsx#L615
const isModelPluginEnabled = api.isPluginEnable('model');
const modelPluginCompat = isModelPluginEnabled && antdConfigSetter;

api.writeTmpFile({
path: `runtime.tsx`,
context: {
Expand All @@ -319,6 +332,7 @@ export default (api: IApi) => {
// 是否启用了 v5 的 theme algorithm
enableV5ThemeAlgorithm,
antdConfigSetter,
modelPluginCompat,
lodashPath,
/**
* 是否重构了全局静态配置。 重构后需要在运行时将全局静态配置传入到 ConfigProvider 中。
Expand Down
5 changes: 5 additions & 0 deletions packages/plugins/templates/antd/runtime.ts.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ const getAntdConfig = () => {
{{/appConfig}}
},
});
{{#modelPluginCompat}}
if (!cacheAntdConfig.theme) {
cacheAntdConfig.theme = {};
}
{{/modelPluginCompat}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

antd 的 config provider 中,如果不配置 theme ,会 fallback 到 || {} ,所以这里使用空对象不会影响到什么。

}
return cacheAntdConfig;
}
Expand Down
Loading