-
Notifications
You must be signed in to change notification settings - Fork 112
/
App.tsx
125 lines (104 loc) · 2.81 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import React from 'react';
import {
Linking,
Platform,
StyleSheet,
Text,
View
} from 'react-native';
import {
Button,
Icon,
} from 'react-native-elements'
import { NavigationContainer, useNavigationContainerRef } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import {HeaderBackButton} from '@react-navigation/elements';
import HomeApp from "./src/home/HomeApp";
import HelloWorldApp from "./src/hello-world/HelloWorldApp";
import AdvancedApp from "./src/advanced/AdvancedApp";
import AsyncStorage from '@react-native-async-storage/async-storage';
const NAVIGATION_KEY = "@transistorsoft:navigation";
const MainStack = createNativeStackNavigator();
// Custom back-button. react-navigation does something messed up with default goBack() action.
const screenOptions = ({ navigation }) => {
return {
headerShown: false
};
}
const App = () => {
const [isReady, setIsReady] = React.useState(false);
const [initialState, setInitialState] = React.useState();
React.useEffect(() => {
const restoreState = async () => {
try {
const initialUrl = await Linking.getInitialURL();
if (Platform.OS !== 'web' && initialUrl == null) {
// Only restore state if there's no deep link and we're not on web
const savedStateString = await AsyncStorage.getItem(NAVIGATION_KEY);
const state = savedStateString ? JSON.parse(savedStateString) : undefined;
if (state !== undefined) {
setInitialState(state);
}
}
} finally {
setIsReady(true);
}
};
if (!isReady) {
restoreState();
}
}, [isReady]);
if (!isReady) {
return null;
}
return (
<NavigationContainer
initialState={initialState}
onStateChange={(state) => {
AsyncStorage.setItem(NAVIGATION_KEY, JSON.stringify(state));
}}
>
<MainStack.Navigator
initialRouteName="HomeApp"
screenOptions={{
headerStyle: {
backgroundColor: '#fedd1e'
},
}}>
<MainStack.Screen
name="HomeApp"
component={HomeApp}
options={{
headerShown: false
}}
/>
<MainStack.Screen
name="AdvancedApp"
component={AdvancedApp}
options={screenOptions}
/>
<MainStack.Screen
name="HelloWorldApp"
component={HelloWorldApp}
options={screenOptions}
/>
</MainStack.Navigator>
</NavigationContainer>
);
};
const styles = StyleSheet.create({
button: {
backgroundColor: 'transparent'
},
input: {
height: 40,
margin: 12,
borderWidth: 1,
borderColor: '#000',
padding: 10,
},
highlight: {
fontWeight: '700',
},
});
export default App;