forked from react-navigation/react-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithCachedChildNavigation.js
More file actions
67 lines (56 loc) · 1.64 KB
/
Copy pathwithCachedChildNavigation.js
File metadata and controls
67 lines (56 loc) · 1.64 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
/* @flow */
import React, { PureComponent } from 'react';
import addNavigationHelpers from './addNavigationHelpers';
import type { NavigationScreenProp, NavigationAction } from './TypeDefinition';
type InjectedProps<N> = {
childNavigationProps: {
[key: string]: N,
},
};
/**
* HOC which caches the child navigation items.
*/
export default function withCachedChildNavigation<T: *, N: *>(
Comp: ReactClass<T & InjectedProps<N>>
): ReactClass<T> {
return class extends PureComponent {
static displayName = `withCachedChildNavigation(${Comp.displayName ||
Comp.name})`;
props: T;
componentWillMount() {
this._updateNavigationProps(this.props.navigation);
}
componentWillReceiveProps(nextProps: T) {
this._updateNavigationProps(nextProps.navigation);
}
_childNavigationProps: {
[key: string]: NavigationScreenProp<N, NavigationAction>,
};
_updateNavigationProps = (
navigation: NavigationScreenProp<N, NavigationAction>
) => {
// Update props for each child route
if (!this._childNavigationProps) {
this._childNavigationProps = {};
}
navigation.state.routes.forEach((route: *) => {
const childNavigation = this._childNavigationProps[route.key];
if (childNavigation && childNavigation.state === route) {
return;
}
this._childNavigationProps[route.key] = addNavigationHelpers({
...navigation,
state: route,
});
});
};
render() {
return (
<Comp
{...this.props}
childNavigationProps={this._childNavigationProps}
/>
);
}
};
}