|
| 1 | +const staticCode = `import { Text, View } from 'react-native'; |
| 2 | +import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; |
| 3 | +import { Provider, BottomNavigation } from 'react-native-paper'; |
| 4 | +import MaterialCommunityIcons from '@expo/vector-icons/MaterialCommunityIcons'; |
| 5 | +import { |
| 6 | + CommonActions, |
| 7 | + createStaticNavigation, |
| 8 | +} from '@react-navigation/native'; |
| 9 | +
|
| 10 | +function HomeScreen() { |
| 11 | + return ( |
| 12 | + <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
| 13 | + <Text>Home!</Text> |
| 14 | + </View> |
| 15 | + ); |
| 16 | +} |
| 17 | +
|
| 18 | +function SettingsScreen() { |
| 19 | + return ( |
| 20 | + <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
| 21 | + <Text>Settings!</Text> |
| 22 | + </View> |
| 23 | + ); |
| 24 | +} |
| 25 | +
|
| 26 | +const MyTabs = createBottomTabNavigator({ |
| 27 | + screenOptions: { |
| 28 | + animation: 'shift', |
| 29 | + }, |
| 30 | + tabBar: ({ navigation, state, descriptors, insets }) => ( |
| 31 | + <BottomNavigation.Bar |
| 32 | + navigationState={state} |
| 33 | + safeAreaInsets={insets} |
| 34 | + onTabPress={({ route, preventDefault }) => { |
| 35 | + const event = navigation.emit({ |
| 36 | + type: 'tabPress', |
| 37 | + target: route.key, |
| 38 | + canPreventDefault: true, |
| 39 | + }); |
| 40 | +
|
| 41 | + if (event.defaultPrevented) { |
| 42 | + preventDefault(); |
| 43 | + } else { |
| 44 | + navigation.dispatch({ |
| 45 | + ...CommonActions.navigate(route.name, route.params), |
| 46 | + target: state.key, |
| 47 | + }); |
| 48 | + } |
| 49 | + }} |
| 50 | + renderIcon={({ route, focused, color }) => |
| 51 | + descriptors[route.key].options.tabBarIcon?.({ |
| 52 | + focused, |
| 53 | + color, |
| 54 | + size: 24, |
| 55 | + }) || null |
| 56 | + } |
| 57 | + getLabelText={({ route }) => { |
| 58 | + const { options } = descriptors[route.key]; |
| 59 | + const label = |
| 60 | + typeof options.tabBarLabel === 'string' |
| 61 | + ? options.tabBarLabel |
| 62 | + : typeof options.title === 'string' |
| 63 | + ? options.title |
| 64 | + : route.name; |
| 65 | +
|
| 66 | + return label; |
| 67 | + }} |
| 68 | + /> |
| 69 | + ), |
| 70 | + screens: { |
| 71 | + Home: { |
| 72 | + screen: HomeScreen, |
| 73 | + options: { |
| 74 | + tabBarIcon: ({ color }) => ( |
| 75 | + <MaterialCommunityIcons name="home" color={color} size={26} /> |
| 76 | + ), |
| 77 | + }, |
| 78 | + }, |
| 79 | + Settings: { |
| 80 | + screen: SettingsScreen, |
| 81 | + options: { |
| 82 | + tabBarIcon: ({ color }) => ( |
| 83 | + <MaterialCommunityIcons name="cog" color={color} size={26} /> |
| 84 | + ), |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | +}); |
| 89 | +
|
| 90 | +const Navigation = createStaticNavigation(MyTabs); |
| 91 | +
|
| 92 | +export default function App() { |
| 93 | + return ( |
| 94 | + <Provider> |
| 95 | + <Navigation> |
| 96 | + <MyTabs /> |
| 97 | + </Navigation> |
| 98 | + </Provider> |
| 99 | + ); |
| 100 | +}`; |
| 101 | + |
| 102 | +const dynamicCode = `import { Text, View } from 'react-native'; |
| 103 | +import { NavigationContainer, CommonActions } from '@react-navigation/native'; |
| 104 | +import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; |
| 105 | +import { Provider, BottomNavigation } from 'react-native-paper'; |
| 106 | +import MaterialCommunityIcons from '@expo/vector-icons/MaterialCommunityIcons'; |
| 107 | +
|
| 108 | +function HomeScreen() { |
| 109 | + return ( |
| 110 | + <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
| 111 | + <Text>Home!</Text> |
| 112 | + </View> |
| 113 | + ); |
| 114 | +} |
| 115 | +
|
| 116 | +function SettingsScreen() { |
| 117 | + return ( |
| 118 | + <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
| 119 | + <Text>Settings!</Text> |
| 120 | + </View> |
| 121 | + ); |
| 122 | +} |
| 123 | +
|
| 124 | +const Tab = createBottomTabNavigator(); |
| 125 | +
|
| 126 | +export default function App() { |
| 127 | + return ( |
| 128 | + <Provider> |
| 129 | + <NavigationContainer> |
| 130 | + <Tab.Navigator |
| 131 | + screenOptions={{ |
| 132 | + animation: 'shift', |
| 133 | + }} |
| 134 | + tabBar={({ navigation, state, descriptors, insets }) => ( |
| 135 | + <BottomNavigation.Bar |
| 136 | + navigationState={state} |
| 137 | + safeAreaInsets={insets} |
| 138 | + onTabPress={({ route, preventDefault }) => { |
| 139 | + const event = navigation.emit({ |
| 140 | + type: 'tabPress', |
| 141 | + target: route.key, |
| 142 | + canPreventDefault: true, |
| 143 | + }); |
| 144 | +
|
| 145 | + if (event.defaultPrevented) { |
| 146 | + preventDefault(); |
| 147 | + } else { |
| 148 | + navigation.dispatch({ |
| 149 | + ...CommonActions.navigate(route.name, route.params), |
| 150 | + target: state.key, |
| 151 | + }); |
| 152 | + } |
| 153 | + }} |
| 154 | + renderIcon={({ route, focused, color }) => |
| 155 | + descriptors[route.key].options.tabBarIcon?.({ |
| 156 | + focused, |
| 157 | + color, |
| 158 | + size: 24, |
| 159 | + }) || null |
| 160 | + } |
| 161 | + getLabelText={({ route }) => { |
| 162 | + const { options } = descriptors[route.key]; |
| 163 | + const label = |
| 164 | + typeof options.tabBarLabel === 'string' |
| 165 | + ? options.tabBarLabel |
| 166 | + : typeof options.title === 'string' |
| 167 | + ? options.title |
| 168 | + : route.name; |
| 169 | +
|
| 170 | + return label; |
| 171 | + }} |
| 172 | + /> |
| 173 | + )}> |
| 174 | + <Tab.Screen |
| 175 | + name="Home" |
| 176 | + component={HomeScreen} |
| 177 | + options={{ |
| 178 | + tabBarIcon: ({ color }) => ( |
| 179 | + <MaterialCommunityIcons name="home" color={color} size={26} /> |
| 180 | + ), |
| 181 | + }} |
| 182 | + /> |
| 183 | + <Tab.Screen |
| 184 | + name="Settings" |
| 185 | + component={SettingsScreen} |
| 186 | + options={{ |
| 187 | + tabBarIcon: ({ color }) => ( |
| 188 | + <MaterialCommunityIcons name="cog" color={color} size={26} /> |
| 189 | + ), |
| 190 | + }} |
| 191 | + /> |
| 192 | + </Tab.Navigator> |
| 193 | + </NavigationContainer> |
| 194 | + </Provider> |
| 195 | + ); |
| 196 | +} |
| 197 | +`; |
| 198 | + |
| 199 | +module.exports = { |
| 200 | + staticCode, |
| 201 | + dynamicCode, |
| 202 | +}; |
0 commit comments