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