Skip to content

Commit 51329dc

Browse files
committed
⚡ perf: test remove hook
1 parent 1f71391 commit 51329dc

File tree

3 files changed

+52
-97
lines changed

3 files changed

+52
-97
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,66 @@
1-
import { ConfigProvider, message, Modal, notification, theme, ThemeConfig } from 'antd';
2-
import { memo, useEffect, useMemo, type FC } from 'react';
1+
import { ConfigProvider, theme, ThemeConfig } from 'antd';
2+
import { memo, useLayoutEffect, useMemo, type FC } from 'react';
33

44
import { useThemeMode } from '@/hooks';
55
import type { ThemeProviderProps } from './type';
66

7-
type AntdProviderProps = Pick<
8-
ThemeProviderProps<any>,
9-
'theme' | 'prefixCls' | 'getStaticInstance' | 'children' | 'staticInstanceConfig'
10-
>;
7+
type AntdProviderProps = Pick<ThemeProviderProps<any>, 'theme' | 'prefixCls' | 'children'>;
118

12-
const AntdProvider: FC<AntdProviderProps> = memo(
13-
({ children, theme: themeProp, prefixCls, getStaticInstance, staticInstanceConfig }) => {
14-
const { appearance, isDarkMode } = useThemeMode();
9+
const Provider: FC<AntdProviderProps> = memo(({ children, theme: themeProp, prefixCls }) => {
10+
const { appearance, isDarkMode } = useThemeMode();
1511

16-
const [messageInstance, messageContextHolder] = message.useMessage(
17-
staticInstanceConfig?.message,
18-
);
19-
const [notificationInstance, notificationContextHolder] = notification.useNotification(
20-
staticInstanceConfig?.notification,
21-
);
22-
const [modalInstance, modalContextHolder] = Modal.useModal();
12+
// 获取 antd 主题
13+
const antdTheme = useMemo<ThemeConfig>(() => {
14+
const baseAlgorithm = isDarkMode ? theme.darkAlgorithm : theme.defaultAlgorithm;
2315

24-
useEffect(() => {
25-
getStaticInstance?.({
26-
message: messageInstance,
27-
modal: modalInstance,
28-
notification: notificationInstance,
29-
});
30-
}, []);
16+
let antdTheme = themeProp as ThemeConfig | undefined;
3117

32-
// 获取 antd 主题
33-
const antdTheme = useMemo<ThemeConfig>(() => {
34-
const baseAlgorithm = isDarkMode ? theme.darkAlgorithm : theme.defaultAlgorithm;
18+
if (typeof themeProp === 'function') {
19+
antdTheme = themeProp(appearance);
20+
}
3521

36-
let antdTheme = themeProp as ThemeConfig | undefined;
22+
if (!antdTheme) {
23+
return { algorithm: baseAlgorithm };
24+
}
3725

38-
if (typeof themeProp === 'function') {
39-
antdTheme = themeProp(appearance);
40-
}
26+
// 如果有 themeProp 说明是外部传入的 theme,需要对算法做一个合并处理,因此先把 themeProp 的算法规整为一个数组
27+
const algoProp = !antdTheme.algorithm
28+
? []
29+
: antdTheme.algorithm instanceof Array
30+
? antdTheme.algorithm
31+
: [antdTheme.algorithm];
4132

42-
if (!antdTheme) {
43-
return { algorithm: baseAlgorithm };
44-
}
33+
return {
34+
...antdTheme,
35+
algorithm: !antdTheme.algorithm ? baseAlgorithm : [baseAlgorithm, ...algoProp],
36+
};
37+
}, [themeProp, isDarkMode]);
4538

46-
// 如果有 themeProp 说明是外部传入的 theme,需要对算法做一个合并处理,因此先把 themeProp 的算法规整为一个数组
47-
const algoProp = !antdTheme.algorithm
48-
? []
49-
: antdTheme.algorithm instanceof Array
50-
? antdTheme.algorithm
51-
: [antdTheme.algorithm];
39+
return (
40+
<ConfigProvider prefixCls={prefixCls} theme={antdTheme}>
41+
{children}
42+
</ConfigProvider>
43+
);
44+
});
5245

53-
return {
54-
...antdTheme,
55-
algorithm: !antdTheme.algorithm ? baseAlgorithm : [baseAlgorithm, ...algoProp],
56-
};
57-
}, [themeProp, isDarkMode]);
46+
Provider.displayName = 'AntdProvider';
5847

59-
return (
60-
<ConfigProvider prefixCls={prefixCls} theme={antdTheme}>
61-
{messageContextHolder}
62-
{notificationContextHolder}
63-
{modalContextHolder}
64-
{children}
65-
</ConfigProvider>
66-
);
67-
},
68-
);
48+
const AntdProvider = memo<AntdProviderProps>(({ children, theme, prefixCls }) => {
49+
useLayoutEffect(() => {
50+
ConfigProvider.config({
51+
holderRender: (children) => (
52+
<Provider theme={theme} prefixCls={prefixCls}>
53+
{children}
54+
</Provider>
55+
),
56+
});
57+
}, [prefixCls, theme]);
6958

70-
AntdProvider.displayName = 'AntdProvider';
59+
return (
60+
<Provider theme={theme} prefixCls={prefixCls}>
61+
{children}
62+
</Provider>
63+
);
64+
});
7165

7266
export default AntdProvider;

src/factories/createThemeProvider/index.tsx

+1-8
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ export const createThemeProvider = (
5151
customStylish,
5252

5353
theme,
54-
getStaticInstance,
5554
prefixCls: outPrefixCls,
56-
staticInstanceConfig,
5755

5856
appearance,
5957
defaultAppearance,
@@ -94,12 +92,7 @@ export const createThemeProvider = (
9492
onAppearanceChange={onAppearanceChange}
9593
useTheme={option.useTheme}
9694
>
97-
<AntdProvider
98-
prefixCls={prefixCls}
99-
staticInstanceConfig={staticInstanceConfig}
100-
theme={theme}
101-
getStaticInstance={getStaticInstance}
102-
>
95+
<AntdProvider prefixCls={prefixCls} theme={theme}>
10396
<TokenContainer
10497
prefixCls={prefixCls}
10598
customToken={customToken}

src/factories/createThemeProvider/type.ts

+2-34
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import {
77
ThemeMode,
88
} from '@/types';
99
import { ThemeConfig } from 'antd/es/config-provider/context';
10-
import { ConfigOptions as MessageConfig, MessageInstance } from 'antd/es/message/interface';
11-
import { ModalStaticFunctions } from 'antd/es/modal/confirm';
12-
import { NotificationConfig, NotificationInstance } from 'antd/es/notification/interface';
10+
import { ConfigOptions as MessageConfig } from 'antd/es/message/interface';
11+
import { NotificationConfig } from 'antd/es/notification/interface';
1312
import { ReactNode } from 'react';
1413

1514
export interface ThemeProviderProps<T, S = Record<string, string>> {
@@ -32,12 +31,6 @@ export interface ThemeProviderProps<T, S = Record<string, string>> {
3231
*/
3332
theme?: ThemeConfig | GetAntdTheme;
3433

35-
/**
36-
* 从 ThemeProvider 中获取静态方法的实例对象
37-
* @param instances
38-
*/
39-
getStaticInstance?: (instances: StaticInstance) => void;
40-
4134
/**
4235
* 静态方法的入参
4336
*/
@@ -62,28 +55,3 @@ export interface ThemeProviderProps<T, S = Record<string, string>> {
6255
defaultThemeMode?: ThemeMode;
6356
onThemeModeChange?: (mode: ThemeMode) => void;
6457
}
65-
66-
/**
67-
* 静态实例
68-
*/
69-
export interface StaticInstance {
70-
/**
71-
* 消息实例
72-
*/
73-
message: MessageInstance;
74-
/**
75-
* 通知实例
76-
*/
77-
notification: NotificationInstance;
78-
/**
79-
* 弹窗实例,不包含 warn 方法
80-
* @typedef {object} Omit<ModalStaticFunctions, 'warn'>
81-
* @property {Function} info - info 弹窗
82-
* @property {Function} success - success 弹窗
83-
* @property {Function} error - error 弹窗
84-
* @property {Function} warning - warning 弹窗
85-
* @property {Function} confirm - confirm 弹窗
86-
* @property {Function} destroyAll - 关闭所有弹窗
87-
*/
88-
modal: Omit<ModalStaticFunctions, 'warn'>;
89-
}

0 commit comments

Comments
 (0)