Quellcode durchsuchen

Add native stack example

Janic Duplessis vor 4 Jahren
Ursprung
Commit
67616426be
2 geänderte Dateien mit 83 neuen und 0 gelöschten Zeilen
  1. 9
    0
      example/src/App.tsx
  2. 74
    0
      example/src/NativeStackExample.tsx

+ 9
- 0
example/src/App.tsx Datei anzeigen

@@ -1,10 +1,14 @@
1 1
 import 'react-native-gesture-handler';
2 2
 import * as React from 'react';
3 3
 import { DevSettings } from 'react-native';
4
+import { enableScreens } from 'react-native-screens';
4 5
 import AsyncStorage from '@react-native-community/async-storage';
5 6
 import ReactNavigation4Example from './ReactNavigation4Example';
6 7
 import ReactNavigation5Example from './ReactNavigation5Example';
7 8
 import SimpleExample from './SimpleExample';
9
+import NativeStackExample from './NativeStackExample';
10
+
11
+enableScreens();
8 12
 
9 13
 const STORAGE_KEY = 'rnsac-current-example';
10 14
 
@@ -40,6 +44,9 @@ export default function App() {
40 44
     DevSettings.addMenuItem('Show React Navigation 5 Example', () => {
41 45
       setCurrentExample('react-navigation-5');
42 46
     });
47
+    DevSettings.addMenuItem('Show Native Stack Example', () => {
48
+      setCurrentExample('native-stack');
49
+    });
43 50
   }, []);
44 51
 
45 52
   switch (currentExample) {
@@ -49,6 +56,8 @@ export default function App() {
49 56
       return <ReactNavigation4Example />;
50 57
     case 'react-navigation-5':
51 58
       return <ReactNavigation5Example />;
59
+    case 'native-stack':
60
+      return <NativeStackExample />;
52 61
     default:
53 62
       return null;
54 63
   }

+ 74
- 0
example/src/NativeStackExample.tsx Datei anzeigen

@@ -0,0 +1,74 @@
1
+import * as React from 'react';
2
+import { Text, View, Button } from 'react-native';
3
+import { NavigationContainer } from '@react-navigation/native';
4
+import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
5
+import { createNativeStackNavigator } from 'react-native-screens/native-stack';
6
+import type { StackScreenProps } from '@react-navigation/stack';
7
+
8
+type Routes = {
9
+  Home: undefined;
10
+  Details: undefined;
11
+  Settings: undefined;
12
+};
13
+
14
+function HomeScreen({ navigation }: StackScreenProps<Routes, 'Home'>) {
15
+  return (
16
+    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
17
+      <Text>Home Screen</Text>
18
+      <Button
19
+        title="Go to Details"
20
+        onPress={() => navigation.navigate('Details')}
21
+      />
22
+    </View>
23
+  );
24
+}
25
+
26
+function SettingsScreen({ navigation }: StackScreenProps<Routes, 'Settings'>) {
27
+  return (
28
+    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
29
+      <Text>Settings Screen</Text>
30
+      <Button
31
+        title="Go to Details"
32
+        onPress={() => navigation.navigate('Details')}
33
+      />
34
+    </View>
35
+  );
36
+}
37
+
38
+const Tab = createBottomTabNavigator();
39
+
40
+function TabsScreen() {
41
+  return (
42
+    <Tab.Navigator>
43
+      <Tab.Screen name="Home" component={HomeScreen} />
44
+      <Tab.Screen name="Settings" component={SettingsScreen} />
45
+    </Tab.Navigator>
46
+  );
47
+}
48
+
49
+function DetailsScreen({ navigation }: StackScreenProps<Routes, 'Details'>) {
50
+  return (
51
+    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
52
+      <Text>Details Screen</Text>
53
+      <Button
54
+        title="Go to Details... again"
55
+        onPress={() => navigation.push('Details')}
56
+      />
57
+      <Button title="Go to Home" onPress={() => navigation.navigate('Home')} />
58
+      <Button title="Go back" onPress={() => navigation.goBack()} />
59
+    </View>
60
+  );
61
+}
62
+
63
+const Stack = createNativeStackNavigator();
64
+
65
+export default function NativeStackExample() {
66
+  return (
67
+    <NavigationContainer>
68
+      <Stack.Navigator>
69
+        <Stack.Screen name="Tabs" component={TabsScreen} />
70
+        <Stack.Screen name="Details" component={DetailsScreen} />
71
+      </Stack.Navigator>
72
+    </NavigationContainer>
73
+  );
74
+}