1
1
import React , { PropTypes } from 'react'
2
+ import { LocationSubscriber } from './locationEmission'
2
3
import {
3
4
location as locationType ,
4
5
routerContext as routerContextType
@@ -41,8 +42,7 @@ class Link extends React.Component {
41
42
}
42
43
43
44
static contextTypes = {
44
- router : routerContextType , // TODO: This should be required, lazy testers be damned
45
- location : locationType // TODO: This should also be required
45
+ router : routerContextType
46
46
}
47
47
48
48
handleClick = ( event ) => {
@@ -65,53 +65,59 @@ class Link extends React.Component {
65
65
}
66
66
67
67
render ( ) {
68
- const { router } = this . context
69
- const {
70
- to,
71
- style,
72
- activeStyle,
73
- className,
74
- activeClassName,
75
- location,
76
- isActive : getIsActive ,
77
- activeOnlyWhenExact, // eslint-disable-line
78
- ...rest
79
- } = this . props
80
-
81
- const currentLocation = location || this . context . location
82
-
83
- const isActive = getIsActive (
84
- currentLocation ,
85
- createLocationDescriptor ( to ) ,
86
- this . props
87
- )
88
-
89
- // If children is a function, we are using a Function as Children Component
90
- // so useful values will be passed down to the children function.
91
- if ( typeof rest . children == 'function' ) {
92
- return rest . children ( {
93
- isActive,
94
- location,
95
- href : router ? router . createHref ( to ) : to ,
96
- onClick : this . handleClick ,
97
- transition : this . handleTransition
98
- } )
99
- }
100
-
101
- // Maybe we should use <Match> here? Not sure how the custom `isActive`
102
- // prop would shake out, also, this check happens a LOT so maybe its good
103
- // to optimize here w/ a faster isActive check, so we'd need to bench mark
104
- // any attempt at changing to use <Match>
105
68
return (
106
- < a
107
- { ...rest }
108
- href = { router ? router . createHref ( to ) : to }
109
- onClick = { this . handleClick }
110
- style = { isActive ? { ...style , ...activeStyle } : style }
111
- className = { isActive ?
112
- [ className , activeClassName ] . join ( ' ' ) . trim ( ) : className
113
- }
114
- />
69
+ < LocationSubscriber >
70
+ { ( contextLocation ) => {
71
+ const { router } = this . context
72
+ const {
73
+ to,
74
+ style,
75
+ activeStyle,
76
+ className,
77
+ activeClassName,
78
+ location :propLocation ,
79
+ isActive : getIsActive ,
80
+ activeOnlyWhenExact, // eslint-disable-line
81
+ ...rest
82
+ } = this . props
83
+
84
+ const currentLocation = propLocation || contextLocation
85
+
86
+ const isActive = getIsActive (
87
+ currentLocation ,
88
+ createLocationDescriptor ( to ) ,
89
+ this . props
90
+ )
91
+
92
+ // If children is a function, we are using a Function as Children Component
93
+ // so useful values will be passed down to the children function.
94
+ if ( typeof rest . children == 'function' ) {
95
+ return rest . children ( {
96
+ isActive,
97
+ currentLocation,
98
+ href : router ? router . createHref ( to ) : to ,
99
+ onClick : this . handleClick ,
100
+ transition : this . handleTransition
101
+ } )
102
+ }
103
+
104
+ // Maybe we should use <Match> here? Not sure how the custom `isActive`
105
+ // prop would shake out, also, this check happens a LOT so maybe its good
106
+ // to optimize here w/ a faster isActive check, so we'd need to benchmark
107
+ // any attempt at changing to use <Match>
108
+ return (
109
+ < a
110
+ { ...rest }
111
+ href = { router ? router . createHref ( to ) : to }
112
+ onClick = { this . handleClick }
113
+ style = { isActive ? { ...style , ...activeStyle } : style }
114
+ className = { isActive ?
115
+ [ className , activeClassName ] . join ( ' ' ) . trim ( ) : className
116
+ }
117
+ />
118
+ )
119
+ } }
120
+ </ LocationSubscriber >
115
121
)
116
122
}
117
123
}
0 commit comments