-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
89 lines (74 loc) · 2.48 KB
/
main.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
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
import Exponent from 'exponent';
import React from 'react';
import {ScrollView, Text, StyleSheet, Button} from 'react-native';
import {StackNavigator, TabNavigator, addNavigationHelpers} from 'react-navigation';
import {createStore} from 'redux';
import {Provider, connect} from 'react-redux';
// This function is used to create components.
// It expects a name and a list of optional links to other routes.
// The generated component is connected to Redux to display the content of the store.
// This function is used below to easily generate components.
function createComponent(name, navigateTo = []) {
return connect(state => ({redux: state}))(({navigation, redux}) => (
<ScrollView style={styles.container}>
<Text style={styles.title}>I am component {name}</Text>
{navigateTo.map(route => (
<Button key={route} title={`Go to ${route}`} onPress={() => navigation.navigate(route)} />
))}
<Text style={styles.title}>Store</Text>
<Text style={styles.store}>{JSON.stringify(redux, undefined, 2)}</Text>
</ScrollView>
)
)
}
// The nested StackNavigator
const NestedNavigator = StackNavigator({
StackOne: {screen: createComponent('StackOne', ['StackTwo'])},
StackTwo: {screen: createComponent('StackTwo', ['TabA'])}
});
// Two tabs, second one is a nested StackNavigator
export const MainNavigator = TabNavigator({
TabA: {
screen: createComponent('TabA', ['TabNested'])
},
TabNested: {
screen: NestedNavigator
},
});
// This connects the MainNavigator to Redux to delegate the whole routing
const AppWithNavigationState = connect(
state => ({
nav: state,
}))(({dispatch, nav}) => (
<MainNavigator navigation={addNavigationHelpers({ dispatch, state: nav })} />
));
// Redux setup.
// Creating reducer from the main navigator.
const appReducer = (state, action) => MainNavigator.router.getStateForAction(action, state);
const store = createStore(appReducer);
// This is the component that bootstraps the app.
export class MinimalNestedReduxApp extends React.Component {
render() {
return (
<Provider store={store}>
<AppWithNavigationState />
</Provider>
);
}
}
// Ignore what is below. It is just some basic styling
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 20
},
title: {
backgroundColor: 'rgba(0, 0, 0, 0.1)',
padding: 16,
},
store: {
padding: 6,
backgroundColor: '#eee',
}
});
Exponent.registerRootComponent(MinimalNestedReduxApp);