Skip to content

Commit b8c69a0

Browse files
committed
Put history on context instead of router
1 parent 1e6f318 commit b8c69a0

File tree

8 files changed

+35
-39
lines changed

8 files changed

+35
-39
lines changed

modules/Lifecycle.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var Lifecycle = {
2626
},
2727

2828
contextTypes: {
29-
router: object.isRequired,
29+
history: object.isRequired,
3030
// Nested children receive the route as context, either
3131
// set by the route component using the RouteContext mixin
3232
// or by some other ancestor.
@@ -51,14 +51,14 @@ var Lifecycle = {
5151
'The Lifecycle mixin requires you to define a routerWillLeave method'
5252
);
5353

54-
this.context.router.registerRouteHook(
54+
this.context.history.registerRouteHook(
5555
this._getRoute(),
5656
this.routerWillLeave
5757
);
5858
},
5959

6060
componentWillUnmount() {
61-
this.context.router.unregisterRouteHook(
61+
this.context.history.unregisterRouteHook(
6262
this._getRoute(),
6363
this.routerWillLeave
6464
);

modules/Link.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function isModifiedEvent(event) {
3232
var Link = React.createClass({
3333

3434
contextTypes: {
35-
router: object
35+
history: object
3636
},
3737

3838
propTypes: {
@@ -68,13 +68,13 @@ var Link = React.createClass({
6868
event.preventDefault();
6969

7070
if (allowTransition)
71-
this.context.router.pushState(this.props.state, this.props.to, this.props.query);
71+
this.context.history.pushState(this.props.state, this.props.to, this.props.query);
7272
},
7373

7474
componentWillMount() {
7575
warning(
76-
this.context.router,
77-
'A <Link> should not be rendered outside the context of a router; ' +
76+
this.context.history,
77+
'A <Link> should not be rendered outside the context of history; ' +
7878
'some features including real hrefs, active styling, and navigation ' +
7979
'will not function correctly'
8080
);
@@ -88,14 +88,14 @@ var Link = React.createClass({
8888
onClick: this.handleClick
8989
};
9090

91-
var { router } = this.context;
91+
var { history } = this.context;
9292

9393
// Ignore if rendered outside the context
94-
// of a router, simplifies unit testing.
95-
if (router) {
96-
props.href = router.createHref(to, query);
94+
// of history, simplifies unit testing.
95+
if (history) {
96+
props.href = history.createHref(to, query);
9797

98-
if (router.isActive(to, query)) {
98+
if (history.isActive(to, query)) {
9999
if (props.activeClassName)
100100
props.className += props.className !== '' ? ` ${props.activeClassName}` : props.activeClassName;
101101

modules/Navigation.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ var { object } = React.PropTypes;
2626
var Navigation = {
2727

2828
contextTypes: {
29-
router: object.isRequired
29+
history: object.isRequired
3030
},
3131

3232
transitionTo(pathname, query, state) {
33-
return this.context.router.pushState(state, pathname, query);
33+
return this.context.history.pushState(state, pathname, query);
3434
},
3535

3636
replaceWith(pathname, query, state) {
37-
return this.context.router.replaceState(state, pathname, query);
37+
return this.context.history.replaceState(state, pathname, query);
3838
}
3939

4040
};
@@ -49,8 +49,8 @@ var RouterNavigationMethods = [
4949

5050
RouterNavigationMethods.forEach(function (method) {
5151
Navigation[method] = function () {
52-
var router = this.context.router;
53-
return router[method].apply(router, arguments);
52+
var { history } = this.context;
53+
return history[method].apply(history, arguments);
5454
};
5555
});
5656

modules/Router.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import RoutingContext from './RoutingContext';
55
import useRoutes from './useRoutes';
66
import { routes } from './PropTypes';
77

8-
var { func } = React.PropTypes;
8+
var { func, object } = React.PropTypes;
99

1010
/**
1111
* A <Router> is a high-level API for automatically setting up
@@ -15,22 +15,16 @@ var { func } = React.PropTypes;
1515
var Router = React.createClass({
1616

1717
propTypes: {
18+
history: object,
1819
children: routes,
1920
routes, // alias for children
20-
createHistory: func.isRequired,
2121
createElement: func,
2222
onError: func,
2323
onUpdate: func,
2424
parseQueryString: func,
2525
stringifyQuery: func
2626
},
2727

28-
getDefaultProps() {
29-
return {
30-
createHistory: createHashHistory
31-
};
32-
},
33-
3428
getInitialState() {
3529
return {
3630
location: null,
@@ -50,15 +44,16 @@ var Router = React.createClass({
5044
},
5145

5246
componentWillMount() {
53-
var { children, routes, parseQueryString, stringifyQuery } = this.props;
47+
var { history, children, routes, parseQueryString, stringifyQuery } = this.props;
48+
var createHistory = history ? () => history : createHashHistory;
5449

55-
this.router = useRoutes(this.props.createHistory)({
50+
this.history = useRoutes(createHistory)({
5651
routes: createRoutes(routes || children),
5752
parseQueryString,
5853
stringifyQuery
5954
});
6055

61-
this._unlisten = this.router.listen((error, state) => {
56+
this._unlisten = this.history.listen((error, state) => {
6257
if (error) {
6358
this.handleError(error);
6459
} else {
@@ -75,7 +70,7 @@ var Router = React.createClass({
7570
render() {
7671
return React.createElement(RoutingContext, {
7772
...this.state,
78-
router: this.router,
73+
history: this.history,
7974
createElement: this.props.createElement
8075
});
8176
}

modules/RoutingContext.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var { array, func, object } = React.PropTypes;
1111
var RoutingContext = React.createClass({
1212

1313
propTypes: {
14-
router: object.isRequired,
14+
history: object.isRequired,
1515
createElement: func.isRequired,
1616
location: object.isRequired,
1717
routes: array.isRequired,
@@ -26,13 +26,13 @@ var RoutingContext = React.createClass({
2626
},
2727

2828
childContextTypes: {
29-
router: object.isRequired,
29+
history: object.isRequired,
3030
location: object.isRequired
3131
},
3232

3333
getChildContext() {
3434
return {
35-
router: this.props.router,
35+
history: this.props.history,
3636
location: this.props.location
3737
};
3838
},
@@ -42,7 +42,7 @@ var RoutingContext = React.createClass({
4242
},
4343

4444
render() {
45-
var { router, location, routes, params, components } = this.props;
45+
var { history, location, routes, params, components } = this.props;
4646
var element = null;
4747

4848
if (components) {
@@ -53,7 +53,7 @@ var RoutingContext = React.createClass({
5353
var route = routes[index];
5454
var routeParams = getRouteParams(route, params);
5555
var props = {
56-
router,
56+
history,
5757
location,
5858
params,
5959
route,

modules/State.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ var { object } = React.PropTypes;
2525
var State = {
2626

2727
contextTypes: {
28-
router: object.isRequired
28+
history: object.isRequired
2929
},
3030

3131
isActive(pathname, query) {
32-
return this.context.router.isActive(pathname, query);
32+
return this.context.history.isActive(pathname, query);
3333
}
3434

3535
};

modules/TransitionUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,5 @@ export function runEnterHooks(routes, nextState, callback) {
6161
export function runLeaveHooks(routes) {
6262
for (var i = 0, len = routes.length; i < len; ++i)
6363
if (routes[i].onLeave)
64-
routes[i].onLeave.call(route);
64+
routes[i].onLeave.call(routes[i]);
6565
}

modules/useRoutes.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import matchRoutes from './matchRoutes';
1616
*/
1717
function useRoutes(createHistory) {
1818
return function (options={}) {
19-
var history = useQueries(createHistory)(options);
20-
var { routes, initialState: state } = options;
19+
var { routes, ...historyOptions } = options;
20+
var history = useQueries(createHistory)(historyOptions);
21+
var state;
2122

2223
function isActive(pathname, query) {
2324
return _isActive(pathname, query, state.location, state.routes, state.params);

0 commit comments

Comments
 (0)