Skip to content

Commit 61f0a8c

Browse files
committed
[changed] Deprecate Navigation/State mixins
Fixes #835 Fixes #744
1 parent b8f1600 commit 61f0a8c

11 files changed

+199
-238
lines changed

modules/Navigation.js

+30-22
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1+
var warning = require('react/lib/warning');
12
var PropTypes = require('./PropTypes');
23

4+
function deprecatedMethod(routerMethodName, fn) {
5+
return function () {
6+
warning(
7+
false,
8+
`Router.Navigation is deprecated. Please use this.context.router.${routerMethodName}() instead`
9+
);
10+
11+
return fn.apply(this, arguments);
12+
};
13+
}
14+
315
/**
416
* A mixin for components that modify the URL.
517
*
618
* Example:
719
*
820
* var MyLink = React.createClass({
921
* mixins: [ Router.Navigation ],
10-
* handleClick: function (event) {
22+
* handleClick(event) {
1123
* event.preventDefault();
1224
* this.transitionTo('aRoute', { the: 'params' }, { the: 'query' });
1325
* },
14-
* render: function () {
26+
* render() {
1527
* return (
1628
* <a onClick={this.handleClick}>Click me!</a>
1729
* );
@@ -21,51 +33,47 @@ var PropTypes = require('./PropTypes');
2133
var Navigation = {
2234

2335
contextTypes: {
24-
makePath: PropTypes.func.isRequired,
25-
makeHref: PropTypes.func.isRequired,
26-
transitionTo: PropTypes.func.isRequired,
27-
replaceWith: PropTypes.func.isRequired,
28-
goBack: PropTypes.func.isRequired
36+
router: PropTypes.router.isRequired
2937
},
3038

3139
/**
3240
* Returns an absolute URL path created from the given route
3341
* name, URL parameters, and query values.
3442
*/
35-
makePath: function (to, params, query) {
36-
return this.context.makePath(to, params, query);
37-
},
43+
makePath: deprecatedMethod('makePath', function (to, params, query) {
44+
return this.context.router.makePath(to, params, query);
45+
}),
3846

3947
/**
4048
* Returns a string that may safely be used as the href of a
4149
* link to the route with the given name.
4250
*/
43-
makeHref: function (to, params, query) {
44-
return this.context.makeHref(to, params, query);
45-
},
51+
makeHref: deprecatedMethod('makeHref', function (to, params, query) {
52+
return this.context.router.makeHref(to, params, query);
53+
}),
4654

4755
/**
4856
* Transitions to the URL specified in the arguments by pushing
4957
* a new URL onto the history stack.
5058
*/
51-
transitionTo: function (to, params, query) {
52-
this.context.transitionTo(to, params, query);
53-
},
59+
transitionTo: deprecatedMethod('transitionTo', function (to, params, query) {
60+
this.context.router.transitionTo(to, params, query);
61+
}),
5462

5563
/**
5664
* Transitions to the URL specified in the arguments by replacing
5765
* the current URL in the history stack.
5866
*/
59-
replaceWith: function (to, params, query) {
60-
this.context.replaceWith(to, params, query);
61-
},
67+
replaceWith: deprecatedMethod('replaceWith', function (to, params, query) {
68+
this.context.router.replaceWith(to, params, query);
69+
}),
6270

6371
/**
6472
* Transitions to the previous URL.
6573
*/
66-
goBack: function () {
67-
return this.context.goBack();
68-
}
74+
goBack: deprecatedMethod('goBack', function () {
75+
return this.context.router.goBack();
76+
})
6977

7078
};
7179

modules/NavigationContext.js

-28
This file was deleted.

modules/PropTypes.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
var assign = require('react/lib/Object.assign');
22
var ReactPropTypes = require('react').PropTypes;
3+
var Route = require('./Route');
34

4-
var PropTypes = assign({
5+
var PropTypes = assign({}, ReactPropTypes, {
56

67
/**
7-
* Requires that the value of a prop be falsy.
8+
* Indicates that a prop should be falsy.
89
*/
910
falsy(props, propName, componentName) {
1011
if (props[propName])
11-
return new Error('<' + componentName + '> may not have a "' + propName + '" prop');
12-
}
12+
return new Error(`<${componentName}> may not have a "${propName}" prop`);
13+
},
1314

14-
}, ReactPropTypes);
15+
/**
16+
* Indicates that a prop should be a Route object.
17+
*/
18+
route: ReactPropTypes.instanceOf(Route),
19+
20+
/**
21+
* Indicates that a prop should be a Router object.
22+
*/
23+
//router: ReactPropTypes.instanceOf(Router) // TODO
24+
router: ReactPropTypes.func
25+
26+
});
1527

1628
module.exports = PropTypes;

modules/RouteHandlerMixin.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ var REF_NAME = '__routeHandler__';
77
var RouteHandlerMixin = {
88

99
contextTypes: {
10-
getRouteAtDepth: PropTypes.func.isRequired,
11-
setRouteComponentAtDepth: PropTypes.func.isRequired,
12-
routeHandlers: PropTypes.array.isRequired
10+
routeHandlers: PropTypes.array.isRequired,
11+
router: PropTypes.router.isRequired
1312
},
1413

1514
childContextTypes: {
@@ -35,15 +34,15 @@ var RouteHandlerMixin = {
3534
},
3635

3736
_updateRouteComponent: function (component) {
38-
this.context.setRouteComponentAtDepth(this.getRouteDepth(), component);
37+
this.context.router.setRouteComponentAtDepth(this.getRouteDepth(), component);
3938
},
4039

4140
getRouteDepth: function () {
4241
return this.context.routeHandlers.length;
4342
},
4443

4544
createChildRouteHandler: function (props) {
46-
var route = this.context.getRouteAtDepth(this.getRouteDepth());
45+
var route = this.context.router.getRouteAtDepth(this.getRouteDepth());
4746
return route ? React.createElement(route.handler, assign({}, props || this.props, { ref: REF_NAME })) : null;
4847
}
4948

modules/State.js

+36-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1+
var warning = require('react/lib/warning');
12
var PropTypes = require('./PropTypes');
23

4+
function deprecatedMethod(routerMethodName, fn) {
5+
return function () {
6+
warning(
7+
false,
8+
`Router.State is deprecated. Please use this.context.router.${routerMethodName}() instead`
9+
);
10+
11+
return fn.apply(this, arguments);
12+
};
13+
}
14+
315
/**
416
* A mixin for components that need to know the path, routes, URL
517
* params and query that are currently active.
@@ -8,7 +20,7 @@ var PropTypes = require('./PropTypes');
820
*
921
* var AboutLink = React.createClass({
1022
* mixins: [ Router.State ],
11-
* render: function () {
23+
* render() {
1224
* var className = this.props.className;
1325
*
1426
* if (this.isActive('about'))
@@ -21,56 +33,51 @@ var PropTypes = require('./PropTypes');
2133
var State = {
2234

2335
contextTypes: {
24-
getCurrentPath: PropTypes.func.isRequired,
25-
getCurrentRoutes: PropTypes.func.isRequired,
26-
getCurrentPathname: PropTypes.func.isRequired,
27-
getCurrentParams: PropTypes.func.isRequired,
28-
getCurrentQuery: PropTypes.func.isRequired,
29-
isActive: PropTypes.func.isRequired
36+
router: PropTypes.router.isRequired
3037
},
3138

3239
/**
3340
* Returns the current URL path.
3441
*/
35-
getPath: function () {
36-
return this.context.getCurrentPath();
37-
},
38-
39-
/**
40-
* Returns an array of the routes that are currently active.
41-
*/
42-
getRoutes: function () {
43-
return this.context.getCurrentRoutes();
44-
},
42+
getPath: deprecatedMethod('getCurrentPath', function () {
43+
return this.context.router.getCurrentPath();
44+
}),
4545

4646
/**
4747
* Returns the current URL path without the query string.
4848
*/
49-
getPathname: function () {
50-
return this.context.getCurrentPathname();
51-
},
49+
getPathname: deprecatedMethod('getCurrentPathname', function () {
50+
return this.context.router.getCurrentPathname();
51+
}),
5252

5353
/**
5454
* Returns an object of the URL params that are currently active.
5555
*/
56-
getParams: function () {
57-
return this.context.getCurrentParams();
58-
},
56+
getParams: deprecatedMethod('getCurrentParams', function () {
57+
return this.context.router.getCurrentParams();
58+
}),
5959

6060
/**
6161
* Returns an object of the query params that are currently active.
6262
*/
63-
getQuery: function () {
64-
return this.context.getCurrentQuery();
65-
},
63+
getQuery: deprecatedMethod('getCurrentQuery', function () {
64+
return this.context.router.getCurrentQuery();
65+
}),
66+
67+
/**
68+
* Returns an array of the routes that are currently active.
69+
*/
70+
getRoutes: deprecatedMethod('getCurrentRoutes', function () {
71+
return this.context.router.getCurrentRoutes();
72+
}),
6673

6774
/**
6875
* A helper method to determine if a given route, params, and query
6976
* are active.
7077
*/
71-
isActive: function (to, params, query) {
72-
return this.context.isActive(to, params, query);
73-
}
78+
isActive: deprecatedMethod('isActive', function (to, params, query) {
79+
return this.context.router.isActive(to, params, query);
80+
})
7481

7582
};
7683

0 commit comments

Comments
 (0)