Skip to content

Commit 08021aa

Browse files
Optimizing ThemeContext
1 parent fc97316 commit 08021aa

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/contexts/themeContext/ThemeContext.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import React, { useContext } from 'react';
2-
3-
import cloneDeep from 'lodash/cloneDeep';
1+
import React, { useContext, useMemo } from 'react';
42
import merge from 'lodash/merge';
53

64
import { defaultTheme, Theme } from './utils/theme';
@@ -18,10 +16,14 @@ export const ThemeContext = React.createContext({} as Theme);
1816
export const ThemeProvider: React.FC<ThemeProviderInputValue> = (props) => {
1917
const { children, style } = props;
2018
const { theme } = useTheme();
21-
const modifiedTheme =
22-
Object.keys(theme).length === 0
23-
? cloneDeep(defaultTheme)
24-
: cloneDeep(theme);
19+
const modifiedTheme = useMemo(
20+
() =>
21+
Object.keys(theme).length === 0
22+
? JSON.parse(JSON.stringify(defaultTheme))
23+
: JSON.parse(JSON.stringify(theme)),
24+
[theme],
25+
);
26+
2527
if (style) {
2628
merge(modifiedTheme, style);
2729
}

0 commit comments

Comments
 (0)