Skip to content

Commit

Permalink
⚡ perf: 优化 createStyles 性能,避免重复渲染
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jan 11, 2023
1 parent ef939cb commit e2eefbd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
48 changes: 25 additions & 23 deletions src/functions/createStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,34 @@ export const createStyles =
(props?: Props): ReturnStyles<Input> => {
const theme = useTheme();

let styles: ReturnStyleToUse<Input>;
const styles = useMemo(() => {
let tempStyles: ReturnStyleToUse<Input>;

if (styleOrGetStyleFn instanceof Function) {
const { stylish, appearance, ...token } = theme;
if (styleOrGetStyleFn instanceof Function) {
const { stylish, appearance, ...token } = theme;

styles = styleOrGetStyleFn(
{ token, stylish, appearance, cx, css, injectGlobal },
props,
) as any;
} else {
styles = styleOrGetStyleFn as any;
}
tempStyles = styleOrGetStyleFn(
{ token, stylish, appearance, cx, css, injectGlobal },
props,
) as any;
} else {
tempStyles = styleOrGetStyleFn as any;
}

if (typeof styles === 'object') {
styles = Object.fromEntries(
Object.entries(styles).map(([key, value]) => {
if (typeof value === 'object') {
return [key, css(value as CSSObject)];
}
if (typeof tempStyles === 'object') {
tempStyles = Object.fromEntries(
Object.entries(tempStyles).map(([key, value]) => {
if (typeof value === 'object') {
return [key, css(value as CSSObject)];
}

return [key, value];
}),
) as any;
}
return [key, value];
}),
) as any;
}

return useMemo(() => {
return { styles, cx, theme };
}, [styles, theme, props]);
return tempStyles;
}, [styleOrGetStyleFn, props, theme]);

return useMemo(() => ({ styles, cx, theme }), [styles, theme]);
};
3 changes: 2 additions & 1 deletion src/hooks/useAntdTheme.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { AntdTheme } from '@/types';
import { useMemo } from 'react';
import { useAntdStylish } from './useAntdStylish';
import { useAntdToken } from './useAntdToken';

export const useAntdTheme = (): AntdTheme => {
const token = useAntdToken();
const antdStylish = useAntdStylish();

return { stylish: antdStylish, ...token };
return useMemo(() => ({ stylish: antdStylish, ...token }), [token, antdStylish]);
};
5 changes: 4 additions & 1 deletion src/hooks/useTheme.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Theme } from '@/types';
import { useTheme as _useTheme } from '@emotion/react';

import { useMemo } from 'react';
import { useAntdTheme } from './useAntdTheme';
import { useThemeMode } from './useThemeMode';

Expand All @@ -9,9 +10,11 @@ export const useTheme = (): Theme => {
const themeState = useThemeMode();
const defaultTheme = _useTheme();

const initTheme = useMemo(() => ({ ...antdTheme, ...themeState }), [antdTheme, themeState]);

// 如果是个空值,说明没有套 Provider,返回 antdTheme 的默认值
if (Object.keys(defaultTheme).length === 0) {
return { ...antdTheme, ...themeState };
return initTheme;
}

return defaultTheme as Theme;
Expand Down

0 comments on commit e2eefbd

Please sign in to comment.