withCachedChildNavigation.js
1.22 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
import * as React from 'react';
import addNavigationHelpers from './addNavigationHelpers';
/**
* HOC which caches the child navigation items.
*/
export default function withCachedChildNavigation(Comp) {
const displayName = Comp.displayName || Comp.name;
return class extends React.PureComponent {
static displayName = `withCachedChildNavigation(${displayName})`;
componentWillMount() {
this._updateNavigationProps(this.props.navigation);
}
componentWillReceiveProps(nextProps) {
this._updateNavigationProps(nextProps.navigation);
}
_updateNavigationProps = navigation => {
// 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({
dispatch: navigation.dispatch,
state: route
});
});
};
render() {
return <Comp {...this.props} childNavigationProps={this._childNavigationProps} />;
}
};
}