forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoutingContext.js
More file actions
100 lines (81 loc) · 2.62 KB
/
RoutingContext.js
File metadata and controls
100 lines (81 loc) · 2.62 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
import invariant from 'invariant'
import React, { Component } from 'react'
import { isReactChildren } from './RouteUtils'
import getRouteParams from './getRouteParams'
const { array, func, object } = React.PropTypes
/**
* A <RoutingContext> renders the component tree for a given router state
* and sets the history object and the current location in context.
*/
class RoutingContext extends Component {
getChildContext() {
const { history, location } = this.props
return { history, location }
}
createElement(component, props) {
return component == null ? null : this.props.createElement(component, props)
}
render() {
const { history, location, routes, params, components } = this.props
let element = null
if (components) {
element = components.reduceRight((element, components, index) => {
if (components == null)
return element // Don't create new children; use the grandchildren.
const route = routes[index]
const routeParams = getRouteParams(route, params)
const props = {
history,
location,
params,
route,
routeParams,
routes
}
if (isReactChildren(element)) {
props.children = element
} else if (element) {
for (let prop in element)
if (element.hasOwnProperty(prop))
props[prop] = element[prop]
}
if (typeof components === 'object') {
const elements = {}
for (const key in components) {
if (components.hasOwnProperty(key)) {
// Pass through the key as a prop to createElement to allow
// custom createElement functions to know which named component
// they're rendering, for e.g. matching up to fetched data.
elements[key] = this.createElement(components[key], {
key, ...props
})
}
}
return elements
}
return this.createElement(components, props)
}, element)
}
invariant(
element === null || element === false || React.isValidElement(element),
'The root route must render a single element'
)
return element
}
}
RoutingContext.propTypes = {
history: object.isRequired,
createElement: func.isRequired,
location: object.isRequired,
routes: array.isRequired,
params: object.isRequired,
components: array.isRequired
}
RoutingContext.defaultProps = {
createElement: React.createElement
}
RoutingContext.childContextTypes = {
history: object.isRequired,
location: object.isRequired
}
export default RoutingContext