-
Notifications
You must be signed in to change notification settings - Fork 53
/
NavigationRootContainer.js
191 lines (158 loc) · 4.79 KB
/
NavigationRootContainer.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule NavigationRootContainer
* @flow
*/
'use strict';
const AsyncStorage = require('react-native').AsyncStorage;
const Linking = require('react-native').Linking;
const NavigationPropTypes = require('./NavigationPropTypes');
const NavigationStateUtils = require('./NavigationStateUtils');
const Platform = require('react-native').Platform;
const React = require('react');
import type {
NavigationAction,
NavigationParentState,
NavigationReducer,
NavigationRenderer,
} from 'NavigationTypeDefinition';
export type BackAction = {
type: 'BackAction',
};
type Props = {
/*
* The default action to be passed into the reducer when getting the first
* state. Defaults to {type: 'RootContainerInitialAction'}
*/
initialAction: NavigationAction,
/*
* Provide linkingActionMap to instruct the container to subscribe to linking
* events, and use this mapper to convert URIs into actions that your app can
* handle
*/
linkingActionMap: ?((uri: ?string) => NavigationAction),
/*
* Provide this key, and the container will store the navigation state in
* AsyncStorage through refreshes, with the provided key
*/
persistenceKey: ?string,
/*
* A function that will output the latest navigation state as a function of
* the (optional) previous state, and an action
*/
reducer: NavigationReducer,
/*
* Set up the rendering of the app for a given navigation state
*/
renderNavigation: NavigationRenderer,
};
type State = {
navState: ?NavigationParentState,
};
function getBackAction(): BackAction {
return { type: 'BackAction' };
}
const PropTypes = require('prop-types');
class NavigationRootContainer extends React.Component<any, Props, State> {
_handleOpenURLEvent: Function;
props: Props;
state: State;
static propTypes = {
initialAction: NavigationPropTypes.action.isRequired,
linkingActionMap: PropTypes.func,
persistenceKey: PropTypes.string,
reducer: PropTypes.func.isRequired,
renderNavigation: PropTypes.func.isRequired,
};
static defaultProps = {
initialAction: { type: 'RootContainerInitialAction' },
};
static childContextTypes = {
onNavigate: PropTypes.func,
};
static getBackAction = getBackAction;
constructor(props: Props) {
super(props);
let navState = null;
if (!this.props.persistenceKey) {
navState = NavigationStateUtils.getParent(
this.props.reducer(null, props.initialAction)
);
}
this.state = { navState };
}
componentWillMount(): void {
(this: any).handleNavigation = this.handleNavigation.bind(this);
(this: any)._handleOpenURLEvent = this._handleOpenURLEvent.bind(this);
}
componentDidMount(): void {
if (this.props.linkingActionMap) {
Linking.getInitialURL().then(this._handleOpenURL.bind(this));
Platform.OS === 'ios' && Linking.addEventListener('url', this._handleOpenURLEvent);
}
if (this.props.persistenceKey) {
AsyncStorage.getItem(this.props.persistenceKey, (err, storedString) => {
if (err || !storedString) {
this.setState({
// $FlowFixMe - navState is missing properties :(
navState: this.props.reducer(null, this.props.initialAction),
});
return;
}
this.setState({
navState: JSON.parse(storedString),
});
});
}
}
componentWillUnmount(): void {
if (Platform.OS === 'ios') {
Linking.removeEventListener('url', this._handleOpenURLEvent);
}
}
_handleOpenURLEvent(event: {url: string}): void {
this._handleOpenURL(event.url);
}
_handleOpenURL(url: ?string): void {
if (!this.props.linkingActionMap) {
return;
}
const action = this.props.linkingActionMap(url);
if (action) {
this.handleNavigation(action);
}
}
getChildContext(): Object {
return {
onNavigate: this.handleNavigation,
};
}
handleNavigation(action: Object): boolean {
const navState = this.props.reducer(this.state.navState, action);
if (navState === this.state.navState) {
return false;
}
this.setState({
// $FlowFixMe - navState is missing properties :(
navState,
});
if (this.props.persistenceKey) {
AsyncStorage.setItem(this.props.persistenceKey, JSON.stringify(navState));
}
return true;
}
render(): ReactElement {
const navigation = this.props.renderNavigation(
this.state.navState,
this.handleNavigation
);
return navigation;
}
}
module.exports = NavigationRootContainer;