Skip to content

Commit b4963db

Browse files
committed
Make Redirect use the new context API
1 parent 5a2bcdd commit b4963db

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

packages/react-router/modules/Redirect.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,19 @@ import PropTypes from "prop-types";
33
import warning from "warning";
44
import invariant from "invariant";
55
import { createLocation, locationsAreEqual } from "history";
6+
import RouterContext from "./RouterContext";
67
import generatePath from "./generatePath";
78

89
/**
910
* The public API for updating the location programmatically
1011
* with a component.
1112
*/
12-
class Redirect extends React.Component {
13+
class InnerRedirect extends React.Component {
1314
static propTypes = {
1415
computedMatch: PropTypes.object, // private, from <Switch>
1516
push: PropTypes.bool,
1617
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,
2519
router: PropTypes.shape({
2620
history: PropTypes.shape({
2721
push: PropTypes.func.isRequired,
@@ -31,13 +25,17 @@ class Redirect extends React.Component {
3125
}).isRequired
3226
};
3327

28+
static defaultProps = {
29+
push: false
30+
};
31+
3432
isStatic() {
35-
return this.context.router && this.context.router.staticContext;
33+
return this.props.router && this.props.router.staticContext;
3634
}
3735

3836
componentWillMount() {
3937
invariant(
40-
this.context.router,
38+
this.props.router,
4139
"You should not use <Redirect> outside a <Router>"
4240
);
4341

@@ -80,8 +78,7 @@ class Redirect extends React.Component {
8078
}
8179

8280
perform() {
83-
const { history } = this.context.router;
84-
const { push } = this.props;
81+
const { push, router: { history } } = this.props;
8582
const to = this.computeTo(this.props);
8683

8784
if (push) {
@@ -96,4 +93,9 @@ class Redirect extends React.Component {
9693
}
9794
}
9895

96+
const Redirect = props =>
97+
RouterContext.consume(({ router }) => (
98+
<InnerRedirect {...props} router={router} />
99+
));
100+
99101
export default Redirect;

0 commit comments

Comments
 (0)