addNavigationHelpers.js.flow
1.57 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
/**
* @flow
*
* Helpers for navigation.
*/
import type {
NavigationProp,
NavigationParams,
NavigationScreenProp,
NavigationNavigateAction,
} from './TypeDefinition';
import NavigationActions from './NavigationActions';
import invariant from './utils/invariant';
export default function<S: {}>(
navigation: NavigationProp<S>
): NavigationScreenProp<S> {
return {
...navigation,
goBack: (key?: ?string): boolean => {
let actualizedKey: ?string = key;
if (key === undefined && navigation.state.key) {
invariant(
typeof navigation.state.key === 'string',
'key should be a string'
);
actualizedKey = navigation.state.key;
}
return navigation.dispatch(
NavigationActions.back({ key: actualizedKey })
);
},
navigate: (
routeName: string,
params?: NavigationParams,
action?: NavigationNavigateAction
): boolean =>
navigation.dispatch(
NavigationActions.navigate({ routeName, params, action })
),
/**
* For updating current route params. For example the nav bar title and
* buttons are based on the route params.
* This means `setParams` can be used to update nav bar for example.
*/
setParams: (params: NavigationParams): boolean => {
invariant(
navigation.state.key && typeof navigation.state.key === 'string',
'setParams cannot be called by root navigator'
);
const key = navigation.state.key;
return navigation.dispatch(NavigationActions.setParams({ params, key }));
},
};
}