-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
71 lines (63 loc) · 1.94 KB
/
Copy pathApp.js
File metadata and controls
71 lines (63 loc) · 1.94 KB
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
import React from "react";
import { StyleSheet, AsyncStorage, AppState } from "react-native";
import { Root, Text, View, Button } from "native-base";
import { Provider } from "react-redux";
import { createStore, applyMiddleware } from "redux";
import { composeWithDevTools } from "redux-devtools-extension";
import reducers from "./src/reducers";
import AppWithNavigationState from "./src/navigation/";
import { middleware as navMiddleware } from "./src/navigation/redux";
export default class App extends React.Component {
state = {
isLoading: true,
store: createStore(
reducers,
composeWithDevTools(applyMiddleware(navMiddleware))
),
};
componentWillMount() {
try {
AppState.addEventListener('change', this._handleAppStateChange);
AsyncStorage.getItem("savedStore", (err, result) => {
console.log(result);
if(result && result.length) {
const initialStore = JSON.parse(result);
this.setState({
isLoading: false,
store: createStore(reducers, initialStore, composeWithDevTools(applyMiddleware(navMiddleware)))
});
} else {
this.setState({ isLoading: false });
}
});
} catch(e) {
this.setState({ isLoading: false });
}
}
componentWillUnmount() {
AppState.removeEventListener('change', this._handleAppStateChange);
}
_handleAppStateChange = currentAppState => {
const storingValue = JSON.stringify(this.state.store.getState())
AsyncStorage.setItem('savedStore', storingValue);
}
render() {
const { store, isLoading } = this.state;
if (isLoading) return <View style={styles.container}></View>
return (
<Provider store={store}>
<Root>
<AppWithNavigationState />
</Root>
</Provider>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center"
}
});