Skip to content

Instantly share code, notes, and snippets.

@josephajibodu
Created August 14, 2022 08:23
Show Gist options
  • Save josephajibodu/519a2ac54be0b601a63cb330ce0596a2 to your computer and use it in GitHub Desktop.
Save josephajibodu/519a2ac54be0b601a63cb330ce0596a2 to your computer and use it in GitHub Desktop.
React Navigation with Typescript - Part 1
declare global {
namespace ReactNavigation {
interface RootParamList extends RootStackParamList {}
}
}
export type RootStackParamList = {
Home: undefined;
Profile: undefined;
Settings: undefined;
};
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { NavigationContainer, DefaultTheme, DarkTheme } from '@react-navigation/native';
import * as React from 'react';
import { RootStackParamList } from '../types';
import ProfileScreen from '../screens/ProfileScreen';
import HomeScreen from '../screens/HomeScreen';
import SettingScreen from '../screens/SettingScreen';
export default function Navigation() {
return (
<NavigationContainer>
<BottomTabNavigator />
</NavigationContainer>
);
}
const BottomTab = createBottomTabNavigator<RootStackParamList>();
function BottomTabNavigator() {
return (
<BottomTab.Navigator
initialRouteName="Home">
<BottomTab.Screen
name="Home"
component={HomeScreen}
options={{
title: 'Home'
}}
/>
<BottomTab.Screen
name="Profile"
component={ProfileScreen}
options={{
title: 'Profile',
}}
/>
<BottomTab.Screen
name="Settings"
component={SettingScreen}
options={{
title: 'Setting',
}}
/>
</BottomTab.Navigator>
);
}
import { BottomTabScreenProps } from '@react-navigation/bottom-tabs';
import { Button, StyleSheet } from 'react-native';
import EditScreenInfo from '../components/EditScreenInfo';
import { Text, View } from '../components/Themed';
import { RootStackParamList } from '../types';
export default function HomeScreen({ navigation }: BottomTabScreenProps<RootStackParamList, 'Home'>) {
return (
<View style={styles.container}>
<Text style={styles.title}>Home</Text>
<View style={styles.separator} lightColor="#eee" darkColor="rgba(255,255,255,0.1)" />
<EditScreenInfo path="/screens/HomeScreen.tsx" />
<Button onPress={() => navigation.navigate('Settings')} title="Go to Settings" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
title: {
fontSize: 20,
fontWeight: 'bold',
},
separator: {
marginVertical: 30,
height: 1,
width: '80%',
},
});
import { StyleSheet } from 'react-native';
import EditScreenInfo from '../components/EditScreenInfo';
import { Text, View } from '../components/Themed';
export default function ProfileScreen() {
return (
<View style={styles.container}>
<Text style={styles.title}>Profile</Text>
<View style={styles.separator} lightColor="#eee" darkColor="rgba(255,255,255,0.1)" />
<EditScreenInfo path="/screens/ProfileScreen.tsx" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
title: {
fontSize: 20,
fontWeight: 'bold',
},
separator: {
marginVertical: 30,
height: 1,
width: '80%',
},
});
import { StyleSheet } from 'react-native';
import EditScreenInfo from '../components/EditScreenInfo';
import { Text, View } from '../components/Themed';
export default function SettingScreen() {
return (
<View style={styles.container}>
<Text style={styles.title}>Settings</Text>
<View style={styles.separator} lightColor="#eee" darkColor="rgba(255,255,255,0.1)" />
<EditScreenInfo path="/screens/SettingScreen.tsx" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
title: {
fontSize: 20,
fontWeight: 'bold',
},
separator: {
marginVertical: 30,
height: 1,
width: '80%',
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment