我在 React Native 中练习和新手,我真的很喜欢 React 导航。
我一直在玩反应导航并进行自定义设置,就像ErrorBoundary
它工作正常一样。
我创建了一个组件并将其命名为设置,用户可以从该设置组件将按钮切换到暗模式。
我没有在文档中找到如何改变应用程序的阴暗情绪。任何人都可以帮助我,如何从子组件中改变黑暗的情绪?
这是我的路由器组件。我在哪里导航组件。
import { Ionicons } from "@expo/vector-icons";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import { useNavigation } from "@react-navigation/native";
import React, { useContext } from "react";
import { Button, Text, View } from "react-native";
import { ThemeContext } from "styled-components";
import ErrorBoundary from "../components/errorBoundaries";
import ITheme from "../themes/types";
import IssuesRouter from "./issues/components/router";
import Settings from "./settings/Settings";
import StyleGuide from "./styleGuide";
import Itinerary from "./tasks/views/itinerary";
const tabIcon = ({ focused, color, size, route }: any) => {
let iconName;
if (route.name === `Tasks`) {
iconName = focused
? `ios-checkmark-circle`
: `ios-checkmark-circle-outline`;
} else if (route.name === `Issues`) {
iconName = focused ? `ios-warning` : `ios-warning`;
} else if (route.name === `History`) {
iconName = focused ? `ios-list-box` : `ios-list`;
} else if (route.name === `Settings`) {
iconName = focused ? `ios-settings` : `ios-settings`;
}
return <Ionicons name={iconName} size={size} color={color} />;
};
const ApplicationRouter = () => {
const Tab = createBottomTabNavigator();
const theme: ITheme = useContext(ThemeContext);
const HomeScreen = () => {
const { navigate } = useNavigation();
return (
<View
style={{
flex: 1,
backgroundColor: `pink`,
justifyContent: `center`,
alignItems: `center`
}}
>
<Text>Home!</Text>
<Button onPress={() => navigate(`Tasks`)} title="Open Modal" />
</View>
);
};
return (
<ErrorBoundary id="ApplicationRouter">
<Tab.Navigator
screenOptions={({ route }) => ({
tabBarIcon: i =>
tabIcon({
...i,
route
})
})}
tabBarOptions={{
activeTintColor: theme.linkColor,
inactiveTintColor: theme.linkColorInactive,
style: {
paddingTop: 10,
borderTopWidth: 0,
backgroundColor: theme.backgroundColorAlt2
}
}}
lazy={false}
>
<Tab.Screen name="Tasks" component={Itinerary} />
<Tab.Screen name="Issues" component={IssuesRouter} />
<Tab.Screen name="History" component={HomeScreen} />
<Tab.Screen name="Settings" component={Settings} /> //THIS IS MY SETTING COMPONENT WHERE I WANT CHANGE MY DARK MOOD
<Tab.Screen name="Style guide" component={StyleGuide} />
</Tab.Navigator>
</ErrorBoundary>
);
};
export default ApplicationRouter;
这是我的设置组件
import React, { useState } from "react";
import { StyleSheet, Switch, View } from "react-native";
export default function App() {
const [isEnabled, setIsEnabled] = useState(false);
const toggleSwitch = () => setIsEnabled(previousState => !previousState);
return (
<View style={styles.container}>
<Switch
trackColor={{ false: "#767577", true: "#81b0ff" }}
thumbColor={isEnabled ? "#f5dd4b" : "#f4f3f4"}
ios_backgroundColor="#3e3e3e"
onValueChange={toggleSwitch}
value={isEnabled}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center"
}
});