1
1
import React , { Component } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
- import { Route } from 'react-router-dom' ;
3
+ import { Route , withRouter } from 'react-router-dom' ;
4
4
5
5
const isModifiedEvent = ( event ) =>
6
6
! ! ( event . metaKey || event . altKey || event . ctrlKey || event . shiftKey ) ;
7
7
8
- export default class LinkContainer extends Component {
9
- static contextTypes = {
10
- router : PropTypes . shape ( {
11
- history : PropTypes . shape ( {
12
- push : PropTypes . func . isRequired ,
13
- replace : PropTypes . func . isRequired ,
14
- createHref : PropTypes . func . isRequired ,
15
- } ) . isRequired ,
16
- } ) . isRequired ,
17
- } ;
18
-
8
+ export class LinkContainer extends Component {
19
9
static propTypes = {
10
+ history : PropTypes . shape ( {
11
+ push : PropTypes . func . isRequired ,
12
+ replace : PropTypes . func . isRequired ,
13
+ createHref : PropTypes . func . isRequired ,
14
+ } ) . isRequired ,
15
+ location : PropTypes . object ,
16
+ match : PropTypes . object ,
17
+ staticContext : PropTypes . object ,
20
18
children : PropTypes . element . isRequired ,
21
19
onClick : PropTypes . func ,
22
20
replace : PropTypes . bool ,
@@ -58,21 +56,24 @@ export default class LinkContainer extends Component {
58
56
) {
59
57
event . preventDefault ( ) ;
60
58
61
- const { history } = this . context . router ;
62
- const { replace, to } = this . props ;
59
+ const { replace, to, history } = this . props ;
63
60
64
61
if ( replace ) {
65
62
history . replace ( to ) ;
66
63
} else {
67
64
history . push ( to ) ;
68
65
}
69
66
}
70
- }
67
+ } ;
71
68
72
69
render ( ) {
73
70
const {
71
+ history,
72
+ location : _location , // eslint-disable-line no-unused-vars
73
+ match : _match , // eslint-disable-line no-unused-vars
74
+ staticContext : _staticContext , // eslint-disable-line no-unused-vars
74
75
children,
75
- replace, // eslint-disable-line no-unused-vars
76
+ replace, // eslint-disable-line no-unused-vars
76
77
to,
77
78
exact,
78
79
strict,
@@ -84,7 +85,7 @@ export default class LinkContainer extends Component {
84
85
...props ,
85
86
} = this . props ;
86
87
87
- const href = this . context . router . history . createHref (
88
+ const href = history . createHref (
88
89
typeof to === 'string' ? { pathname : to } : to
89
90
) ;
90
91
@@ -114,3 +115,5 @@ export default class LinkContainer extends Component {
114
115
) ;
115
116
}
116
117
}
118
+
119
+ export default withRouter ( LinkContainer ) ;
0 commit comments