@@ -10,27 +10,20 @@ const isModifiedEvent = event =>
10
10
/**
11
11
* The public API for rendering a history-aware <a>.
12
12
*/
13
- class InnerLink extends React . Component {
13
+ class Link extends React . Component {
14
14
static propTypes = {
15
15
onClick : PropTypes . func ,
16
16
target : PropTypes . string ,
17
17
replace : PropTypes . bool ,
18
18
to : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . object ] ) . isRequired ,
19
- innerRef : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . func ] ) ,
20
- router : PropTypes . shape ( {
21
- history : PropTypes . shape ( {
22
- push : PropTypes . func . isRequired ,
23
- replace : PropTypes . func . isRequired ,
24
- createHref : PropTypes . func . isRequired
25
- } ) . isRequired
26
- } ) . isRequired
19
+ innerRef : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . func ] )
27
20
} ;
28
21
29
22
static defaultProps = {
30
23
replace : false
31
24
} ;
32
25
33
- handleClick = event => {
26
+ handleClick = history => event => {
34
27
if ( this . props . onClick ) this . props . onClick ( event ) ;
35
28
36
29
if (
@@ -41,7 +34,7 @@ class InnerLink extends React.Component {
41
34
) {
42
35
event . preventDefault ( ) ;
43
36
44
- const { replace, to, router : { history } } = this . props ;
37
+ const { replace, to } = this . props ;
45
38
46
39
if ( replace ) {
47
40
history . replace ( to ) ;
@@ -52,33 +45,30 @@ class InnerLink extends React.Component {
52
45
} ;
53
46
54
47
render ( ) {
55
- const { replace, to, innerRef, router, ...props } = this . props ; // eslint-disable-line no-unused-vars
56
-
57
- invariant (
58
- this . props . router ,
59
- "You should not use <Link> outside a <Router>"
60
- ) ;
48
+ const { replace, to, innerRef, ...props } = this . props ; // eslint-disable-line no-unused-vars
61
49
62
50
invariant ( to !== undefined , 'You must specify the "to" property' ) ;
63
51
64
- const { history } = router ;
65
- const location =
66
- typeof to === "string"
67
- ? createLocation ( to , null , null , history . location )
68
- : to ;
52
+ return RouterContext . consume ( ( { router } ) => {
53
+ invariant ( router , "You should not use <Link> outside a <Router>" ) ;
54
+
55
+ const { history } = router ;
56
+ const location =
57
+ typeof to === "string"
58
+ ? createLocation ( to , null , null , history . location )
59
+ : to ;
69
60
70
- const href = history . createHref ( location ) ;
71
- return (
72
- < a { ...props } onClick = { this . handleClick } href = { href } ref = { innerRef } />
73
- ) ;
61
+ const href = history . createHref ( location ) ;
62
+ return (
63
+ < a
64
+ { ...props }
65
+ onClick = { this . handleClick ( history ) }
66
+ href = { href }
67
+ ref = { innerRef }
68
+ />
69
+ ) ;
70
+ } ) ;
74
71
}
75
72
}
76
73
77
- export { InnerLink } ;
78
-
79
- const Link = props =>
80
- RouterContext . consume ( ( { router } ) => (
81
- < InnerLink { ...props } router = { router } />
82
- ) ) ;
83
-
84
74
export default Link ;
0 commit comments