@@ -3,25 +3,19 @@ import PropTypes from "prop-types";
3
3
import warning from "warning" ;
4
4
import invariant from "invariant" ;
5
5
import { createLocation , locationsAreEqual } from "history" ;
6
+ import RouterContext from "./RouterContext" ;
6
7
import generatePath from "./generatePath" ;
7
8
8
9
/**
9
10
* The public API for updating the location programmatically
10
11
* with a component.
11
12
*/
12
- class Redirect extends React . Component {
13
+ class InnerRedirect extends React . Component {
13
14
static propTypes = {
14
15
computedMatch : PropTypes . object , // private, from <Switch>
15
16
push : PropTypes . bool ,
16
17
from : PropTypes . string ,
17
- to : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . object ] ) . isRequired
18
- } ;
19
-
20
- static defaultProps = {
21
- push : false
22
- } ;
23
-
24
- static contextTypes = {
18
+ to : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . object ] ) . isRequired ,
25
19
router : PropTypes . shape ( {
26
20
history : PropTypes . shape ( {
27
21
push : PropTypes . func . isRequired ,
@@ -31,13 +25,17 @@ class Redirect extends React.Component {
31
25
} ) . isRequired
32
26
} ;
33
27
28
+ static defaultProps = {
29
+ push : false
30
+ } ;
31
+
34
32
isStatic ( ) {
35
- return this . context . router && this . context . router . staticContext ;
33
+ return this . props . router && this . props . router . staticContext ;
36
34
}
37
35
38
36
componentWillMount ( ) {
39
37
invariant (
40
- this . context . router ,
38
+ this . props . router ,
41
39
"You should not use <Redirect> outside a <Router>"
42
40
) ;
43
41
@@ -80,8 +78,7 @@ class Redirect extends React.Component {
80
78
}
81
79
82
80
perform ( ) {
83
- const { history } = this . context . router ;
84
- const { push } = this . props ;
81
+ const { push, router : { history } } = this . props ;
85
82
const to = this . computeTo ( this . props ) ;
86
83
87
84
if ( push ) {
@@ -96,4 +93,9 @@ class Redirect extends React.Component {
96
93
}
97
94
}
98
95
96
+ const Redirect = props =>
97
+ RouterContext . consume ( ( { router } ) => (
98
+ < InnerRedirect { ...props } router = { router } />
99
+ ) ) ;
100
+
99
101
export default Redirect ;
0 commit comments