-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
60 lines (50 loc) · 1.65 KB
/
App.js
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
import { NavigationContainer } from "@react-navigation/native";
import AppLoading from "expo-app-loading";
import React, { useEffect, useReducer, useState } from "react";
import { Provider as PaperProvider } from "react-native-paper";
import { theme } from "./app/constants";
import AppNavigator from "./app/navigation/AppNavigator";
import navigationTheme from "./app/navigation/navigationTheme";
import { ReduxContext } from "./app/redux/context";
import { initState, reducer } from "./app/redux/reducer";
import cache from "./app/utility/cache";
const PERSISTENCE_KEY = "NAVIGATION_STATE";
export default function App() {
const [isReady, setIsReady] = useState(false);
const [initialState, setInitalState] = useState();
const [state, dispatch] = useReducer(reducer, initState);
const restoreState = async () => {
try {
const state = (await cache.get(PERSISTENCE_KEY)) || undefined;
setInitalState(state);
} finally {
setIsReady(true);
}
if (!isReady) restoreState();
};
useEffect(() => {
restoreState();
}, [isReady]);
const onStateChange = (PERSISTENCE_KEY, state) =>
cache.store(PERSISTENCE_KEY, state);
if (!isReady)
return (
<AppLoading
startAsync={restoreState}
onFinish={() => isReady(true)}
onError={console.warn}
/>
);
return (
<ReduxContext.Provider value={{ state, dispatch }}>
<PaperProvider {...{ theme }}>
<NavigationContainer
{...{ initialState, onStateChange }}
theme={navigationTheme}
>
<AppNavigator />
</NavigationContainer>
</PaperProvider>
</ReduxContext.Provider>
);
}