forked from react-navigation/react-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowserAppContainer.js
More file actions
140 lines (133 loc) · 3.92 KB
/
Copy pathBrowserAppContainer.js
File metadata and controls
140 lines (133 loc) · 3.92 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
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
import React from 'react';
import PropTypes from 'prop-types';
import { NavigationActions, addNavigationHelpers } from 'react-navigation';
function getAction(router, path, params) {
const action = router.getActionForPathAndParams(path, params);
if (action) {
return action;
}
return NavigationActions.navigate({
params: { path },
routeName: 'NotFound',
});
}
export default NavigationAwareView => {
const initialAction = getAction(
NavigationAwareView.router,
window.location.pathname.substr(1)
);
const initialState = NavigationAwareView.router.getStateForAction(
initialAction
);
console.log({ initialAction, initialState });
class NavigationContainer extends React.Component {
state = initialState;
componentDidMount() {
const navigation = addNavigationHelpers({
state: this.state.routes[this.state.index],
dispatch: this.dispatch,
});
document.title = NavigationAwareView.router.getScreenOptions(
navigation
).title;
window.onpopstate = e => {
e.preventDefault();
const action = getAction(
NavigationAwareView.router,
window.location.pathname.substr(1)
);
if (action) this.dispatch(action);
};
}
componentWillUpdate(props, state) {
const {
path,
params,
} = NavigationAwareView.router.getPathAndParamsForState(state);
const maybeHash = params && params.hash ? `#${params.hash}` : '';
const uri = `/${path}${maybeHash}`;
if (window.location.pathname !== uri) {
window.history.pushState({}, state.title, uri);
}
const navigation = addNavigationHelpers({
state: state.routes[state.index],
dispatch: this.dispatch,
});
document.title = NavigationAwareView.router.getScreenOptions(
navigation
).title;
}
componentDidUpdate() {
const { params } = NavigationAwareView.router.getPathAndParamsForState(
this.state
);
if (params && params.hash) {
document.getElementById(params.hash).scrollIntoView();
}
}
dispatch = action => {
const state = NavigationAwareView.router.getStateForAction(
action,
this.state
);
if (!state) {
console.log('Dispatched action did not change state: ', { action });
} else if (console.group) {
console.group('Navigation Dispatch: ');
console.log('Action: ', action);
console.log('New State: ', state);
console.log('Last State: ', this.state);
console.groupEnd();
} else {
console.log('Navigation Dispatch: ', {
action,
newState: state,
lastState: this.state,
});
}
if (!state) {
return true;
}
if (state !== this.state) {
this.setState(state);
return true;
}
return false;
};
render() {
return (
<NavigationAwareView
navigation={addNavigationHelpers({
state: this.state,
dispatch: this.dispatch,
})}
/>
);
}
getURIForAction = action => {
const state =
NavigationAwareView.router.getStateForAction(action, this.state) ||
this.state;
const { path } = NavigationAwareView.router.getPathAndParamsForState(
state
);
return `/${path}`;
};
getActionForPathAndParams = (path, params) => {
return NavigationAwareView.router.getActionForPathAndParams(path, params);
};
static childContextTypes = {
getActionForPathAndParams: PropTypes.func.isRequired,
getURIForAction: PropTypes.func.isRequired,
dispatch: PropTypes.func.isRequired,
};
getChildContext() {
return {
getActionForPathAndParams: this.getActionForPathAndParams,
getURIForAction: this.getURIForAction,
dispatch: this.dispatch,
};
}
}
return NavigationContainer;
};