I am practicing and new in React native and I really like React navigation.
I have been playing around react navigation and made custom settings like ErrorBoundary
which it works fine.
I created one component and named it settings, from that settings component user can toggle the button go to Dark mode.
I didn't find in the documentation how to change the app's dark mood. Can any please help me out, How to change dark mood from child component?
This is my router component. Where I navigate the component.
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;
This is my setting component
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"
}
});