我一直在使用 react native 元素。我想为我的应用程序实现暗模式,但由于某种原因,<ThemeProvider/>
当我在上下文中的状态发生变化时,我无法让主题道具改变。
这是我的上下文,其中有我的 darkTheme 和 lightTheme 对象。我也有一个lightThemeState
using useState,因此我可以从子组件设置该状态。
import React, { createContext, useState, useEffect } from "react";
import { AsyncStorage } from "react-native";
import { ThemeProvider } from "react-native-elements";
import lightTheme from "../themes/light";
import darkTheme from "../themes/dark";
export const ThemeModeContext = createContext();
export const ThemeContextProvider = (props) => {
const [lightThemeState, setLightThemeState] = useState(true);
const saveThemeState = async () => {
if (lightThemeState) {
await AsyncStorage.removeItem("lightThemeState");
} else {
await AsyncStorage.setItem(
"lightThemeState",
JSON.stringify(lightThemeState)
);
}
};
const getThemeState = async () => {
currentMode = await AsyncStorage.getItem("lightThemeState");
if (currentMode) {
setLightThemeState(JSON.parse(currentMode));
}
};
useEffect(() => {
saveThemeState();
}, [lightThemeState]);
useEffect(() => {
getThemeState();
}, []);
const currentTheme = lightThemeState ? lightTheme : darkTheme;
console.log("LIGHT THEME STATE", lightThemeState);
// When I log this after I used the setLigthThemeState in a child component. It gives the correct state ie true or false.
console.log("COLOR OF THE THEMES BACKGROUND", currentTheme.colors.background);
// This also gives the correct background for the theme that is the "currentTheme" depending on the state. So this far, everything is correct.
return (
<ThemeModeContext.Provider value={[lightThemeState, setLightThemeState]}>
<ThemeProvider theme={currentTheme}>{props.children}</ThemeProvider>
</ThemeModeContext.Provider>
);
};
export default ThemeContextProvider;
因为我有另一个用于其他逻辑的上下文。我结合<ThemeContextProvider/>
我的其他上下文<JourneyContextProvider/>
。像这样:
import React from "react";
import ThemeContextProvider from "./themeStore";
import JourneyContextProvider from "./journeyStore";
export const CombinedStoreProvider = (props) => {
return (
<JourneyContextProvider>
<ThemeContextProvider>{props.children}</ThemeContextProvider>
</JourneyContextProvider>
);
};
export default CombinedStoreProvider;
然后最后我将整个应用程序包装在我的<CombinedStoreProvider/>
. 像这样。
import React from "react";
import { SafeAreaView } from "react-native";
import { createAppContainer, createSwitchNavigator } from "react-navigation";
import { createMaterialBottomTabNavigator } from "react-navigation-material-bottom-tabs";
import Icon from "react-native-vector-icons/Ionicons";
import MoreScreenfrom "./src/screens/MoreModal";
import CombinedStoreProvider from "./store/combinedStore";
const TabNavigator = createMaterialBottomTabNavigator(
{
MoreScreen: {
screen: MoreScreen,
navigationOptions: {
title: "More",
tabBarIcon: ({ tintColor }) => (
<SafeAreaView>
<Icon style={[{ color: tintColor }]} size={25} name={"ios-more"} />
</SafeAreaView>
),
},
},
},
{
theme: ({ darkTheme }) => console.log(darkTheme),
barStyleDark: {
backgroundColor: darkTheme.colors.background,
},
barStyleLight: {
backgroundColor: lightTheme.colors.background,
},
shifting: false,
labeled: true,
initialRouteName: "MoreScreen",
activeColor: "#E4DC93",
inactiveColor: "#fff",
barStyle: { backgroundColor: "transparent", height: 80, paddingTop: 10 },
}
);
const AllRoutes = createSwitchNavigator(
{
PersonalSettings: {
title: "Personal Settings",
screen: PersonalSettings,
header: ({ goBack }) => ({
left: (
<Icon
name={"chevron-left"}
onPress={() => {
goBack();
}}
/>
),
}),
},
Tabs: {
screen: TabNavigator,
},
},
{
initialRouteName: "Tabs",
}
);
const AppContainer = createAppContainer(AllRoutes);
export default App = () => {
return (
<CombinedStoreProvider>
<AppContainer />
</CombinedStoreProvider>
);
};
这是我lightThemeState
在上下文中切换的子组件。但即使一切看起来都很棒ThemeContextProvider
(我控制台记录了状态和背景颜色,他们已经成功地改变了主题)。但是在这个组件中,我只得到了以前的主题。即使这个子组件在我切换lightThemeState
. 我知道这一点,因为在我切换主题后控制台登录该组件再次记录,但日志显示以前的主题颜色。这是子组件:
import React, { useContext, useState } from "react";
import { StyleSheet, View, Text } from "react-native";
import { LayoutView, ContainerView } from "../../components/styles";
import { ThemeModeContext } from "../../../store/themeStore";
import { Card, ListItem, Avatar, ThemeContext } from "react-native-elements";
import CustomButton from "../../components/CustomButton";
const INITIAL_PERSONAL_INFO_STATE = {
name: "",
username: "",
profileImage: "",
favoriteDestinations: [],
};
const MoreModal = (props) => {
const [personalInfo, setPersonalInfo] = useState(INITIAL_PERSONAL_INFO_STATE);
const [lightThemeState, setLightThemeState] = useContext(ThemeModeContext);
const { theme } = useContext(ThemeContext);
const { navigate } = props.navigation;
const primaryColor = theme.colors.background;
console.log("COLOR IN COMPONENT", primaryColor);
// The color is from the previous theme and even thou the state has changed in the state below
console.log("LIGHT THEME STATE IN COMPONENT", lightThemeState);
return (
<LayoutView primaryColor={theme.colors.background}>
<ContainerView>
<View>
</View>
<Card
title={"Settings"}
>
<ListItem
title="Light mode"
switch={{
value: lightThemeState,
onValueChange: (value) => setLightThemeState(value),
// Here is where I set lighThemeState to false in my context
}}
bottomDivider
</Card>
</ContainerView>
<CustomButton title={"Sign in"}></CustomButton>
</LayoutView>
);
};
export default MoreModal;
也许你问的darkTheme和lightTheme有问题?不,如果我将状态从 true 更改为 false 并重新加载应用程序。有用。不知何故,主题在<ThemeProvider theme={currentTheme}/>
. 有人可以解释为什么吗?