/
renderRoutes.js
213 lines (202 loc) · 5.24 KB
/
renderRoutes.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import React from 'react';
import { Route, Redirect } from 'react-router-dom';
import CacheRoute, { CacheSwitch } from 'react-router-cache-route';
const RouteInstanceMap = {
get(key) {
return key._routeInternalComponent;
},
has(key) {
return key._routeInternalComponent !== undefined;
},
set(key, value) {
key._routeInternalComponent = value;
},
};
// Support pass props from layout to child routes
const RouteWithProps = ({
path,
exact,
strict,
render,
location,
sensitive,
keepAlive,
...rest
}) => {
if (keepAlive) {
return (
<CacheRoute
when="always"
cacheKey={path}
path={path}
exact={exact}
strict={strict}
location={location}
sensitive={sensitive}
render={props => render({ ...props, ...rest })}
/>
);
}
return (
<Route
path={path}
exact={exact}
strict={strict}
location={location}
sensitive={sensitive}
render={props => render({ ...props, ...rest })}
/>
);
};
function getCompatProps(props) {
const compatProps = {};
if (__UMI_BIGFISH_COMPAT) {
if (props.match && props.match.params && !props.params) {
compatProps.params = props.match.params;
}
}
return compatProps;
}
function withRoutes(route) {
if (RouteInstanceMap.has(route)) {
return RouteInstanceMap.get(route);
}
const { Routes } = route;
let len = Routes.length - 1;
let Component = args => {
const { render, ...props } = args;
return render(props);
};
while (len >= 0) {
const AuthRoute = Routes[len];
const OldComponent = Component;
Component = props => (
<AuthRoute {...props}>
<OldComponent {...props} />
</AuthRoute>
);
len -= 1;
}
const ret = args => {
const { render, ...rest } = args;
return (
<RouteWithProps
{...rest}
render={props => {
return <Component {...props} route={route} render={render} />;
}}
/>
);
};
RouteInstanceMap.set(route, ret);
return ret;
}
function wrapWithInitialProps(WrappedComponent, initialProps) {
return class extends React.Component {
constructor(props) {
super(props);
this.state = {
extraProps: {},
};
}
async componentDidMount() {
const { history } = this.props;
window.onpopstate = () => {
this.getInitialProps();
};
if (history.action !== 'POP') {
this.getInitialProps();
}
}
// 前端路由切换时,也需要执行 getInitialProps
async getInitialProps() {
// the values may be different with findRoute.js
const { match, location } = this.props;
const extraProps = await WrappedComponent.getInitialProps({
isServer: false,
route: match,
location,
...initialProps,
});
this.setState({
extraProps,
});
}
render() {
const { extraProps } = this.state;
return (
<WrappedComponent
{...{
...this.props,
...extraProps,
}}
/>
);
}
};
}
export default function renderRoutes(routes, extraProps = {}, switchProps = {}) {
const plugins = require('umi/_runtimePlugin');
return routes ? (
<CacheSwitch {...switchProps} which={element => element.props.keepAlive}>
{routes.map((route, i) => {
if (route.redirect) {
return (
<Redirect
key={route.key || i}
from={route.path}
to={route.redirect}
exact={route.exact}
strict={route.strict}
/>
);
}
const RouteRoute = route.Routes ? withRoutes(route) : RouteWithProps;
return (
<RouteRoute
key={route.key || i}
path={route.path}
exact={route.exact}
strict={route.strict}
sensitive={route.sensitive}
keepAlive={route.keepAlive}
render={props => {
const childRoutes = renderRoutes(route.routes, extraProps, {
location: props.location,
});
if (route.component) {
const compatProps = getCompatProps({
...props,
...extraProps,
});
const newProps = plugins.apply('modifyRouteProps', {
initialValue: {
...props,
...extraProps,
...compatProps,
},
args: { route },
});
let { component: Component } = route;
// eslint-disable-next-line no-undef
if (__IS_BROWSER && Component.getInitialProps) {
const initialProps = plugins.apply('modifyInitialProps', {
initialValue: {},
});
Component = wrapWithInitialProps(Component, initialProps);
}
return (
<Component {...newProps} route={route}>
{childRoutes}
</Component>
);
} else {
return childRoutes;
}
}}
/>
);
})}
</CacheSwitch>
) : null;
}