Skip to content

Commit

Permalink
fix: undefined components in useTheme hook (react-native-elements#3643
Browse files Browse the repository at this point in the history
)
  • Loading branch information
arpitBhalla authored Sep 21, 2022
1 parent 45d0058 commit 64a1160
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
31 changes: 12 additions & 19 deletions packages/themed/src/config/ThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useContext } from 'react';
import React, { useCallback, useContext, useMemo } from 'react';
import deepmerge from 'deepmerge';
import { Colors, lightColors, darkColors } from './colors';
import {
Expand Down Expand Up @@ -146,31 +146,24 @@ interface UseTheme {
}

export const useTheme = (): UseTheme => {
const {
theme: { components, ...restTheme },
replaceTheme,
updateTheme,
} = useContext(ThemeContext);

return {
theme: restTheme,
replaceTheme,
updateTheme,
};
return useContext(ThemeContext);
};

export const useThemeMode = () => {
const {
updateTheme,
theme: { mode },
} = useTheme();
const themeContext = useContext(ThemeContext);

const setMode = useCallback(
(colorMode: ThemeMode) => {
updateTheme({ mode: colorMode });
themeContext?.updateTheme({ mode: colorMode });
},
[updateTheme]
[themeContext]
);

return { mode, setMode };
return useMemo(
() => ({
mode: themeContext?.theme?.mode,
setMode,
}),
[setMode, themeContext?.theme?.mode]
);
};
5 changes: 3 additions & 2 deletions website/docs/customization/1.themeprovider.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,11 @@ const theme = createTheme({

const ColorScheme = ({ children }) => {
const colorMode = useColorScheme();
const { theme, setThemeMode } = useTheme();
const { theme } = useTheme();
const { setMode } = useThemeMode();

React.useEffect(() => {
setThemeMode(colorMode);
setMode(colorMode);
}, [colorMode]);

return (
Expand Down

0 comments on commit 64a1160

Please sign in to comment.