forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetComponents.js
More file actions
58 lines (49 loc) · 1.89 KB
/
getComponents.js
File metadata and controls
58 lines (49 loc) · 1.89 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
import { mapAsync } from './AsyncUtils'
import { canUseMembrane } from './deprecateObjectProperties'
import warning from './routerWarning'
function getComponentsForRoute(nextState, route, callback) {
if (route.component || route.components) {
callback(null, route.component || route.components)
return
}
const getComponent = route.getComponent || route.getComponents
if (!getComponent) {
callback()
return
}
const { location } = nextState
let nextStateWithLocation
if (__DEV__ && canUseMembrane) {
nextStateWithLocation = { ...nextState }
// I don't use deprecateObjectProperties here because I want to keep the
// same code path between development and production, in that we just
// assign extra properties to the copy of the state object in both cases.
for (const prop in location) {
if (!Object.prototype.hasOwnProperty.call(location, prop)) {
continue
}
Object.defineProperty(nextStateWithLocation, prop, {
get() {
warning(false, 'Accessing location properties from the first argument to `getComponent` and `getComponents` is deprecated. That argument is now the router state (`nextState`) rather than the location. To access the location, use `nextState.location`.')
return location[prop]
}
})
}
} else {
nextStateWithLocation = { ...nextState, ...location }
}
getComponent.call(route, nextStateWithLocation, callback)
}
/**
* Asynchronously fetches all components needed for the given router
* state and calls callback(error, components) when finished.
*
* Note: This operation may finish synchronously if no routes have an
* asynchronous getComponents method.
*/
function getComponents(nextState, callback) {
mapAsync(nextState.routes, function (route, index, callback) {
getComponentsForRoute(nextState, route, callback)
}, callback)
}
export default getComponents