11import React , { Component } from 'react' ;
22import PropTypes from 'prop-types' ;
3- import { Route } from 'react-router-dom' ;
3+ import { Route , withRouter } from 'react-router-dom' ;
44
55const isModifiedEvent = ( event ) =>
66 ! ! ( event . metaKey || event . altKey || event . ctrlKey || event . shiftKey ) ;
77
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 {
199 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 ,
2018 children : PropTypes . element . isRequired ,
2119 onClick : PropTypes . func ,
2220 replace : PropTypes . bool ,
@@ -58,21 +56,24 @@ export default class LinkContainer extends Component {
5856 ) {
5957 event . preventDefault ( ) ;
6058
61- const { history } = this . context . router ;
62- const { replace, to } = this . props ;
59+ const { replace, to, history } = this . props ;
6360
6461 if ( replace ) {
6562 history . replace ( to ) ;
6663 } else {
6764 history . push ( to ) ;
6865 }
6966 }
70- }
67+ } ;
7168
7269 render ( ) {
7370 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
7475 children,
75- replace, // eslint-disable-line no-unused-vars
76+ replace, // eslint-disable-line no-unused-vars
7677 to,
7778 exact,
7879 strict,
@@ -84,7 +85,7 @@ export default class LinkContainer extends Component {
8485 ...props ,
8586 } = this . props ;
8687
87- const href = this . context . router . history . createHref (
88+ const href = history . createHref (
8889 typeof to === 'string' ? { pathname : to } : to
8990 ) ;
9091
@@ -114,3 +115,5 @@ export default class LinkContainer extends Component {
114115 ) ;
115116 }
116117}
118+
119+ export default withRouter ( LinkContainer ) ;
0 commit comments