Skip to content

Commit 85d7bba

Browse files
committed
Calculate router state with updated props, fixes #10
1 parent 1736cbf commit 85d7bba

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

docs/recipes/custom-router.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ We are going to implement custom router as a mixin:
2121
var MyRouterMixin = {
2222
mixins: [Router.RouterMixin, Router.AsyncRouteRenderingMixin],
2323

24-
getRoutes: function() {
24+
getRoutes: function(props) {
2525
var routes = []
2626
for (var path in this.routes)
2727
routes.push({path: path, handler: this.routes[path]})

lib/Router.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ function createRouter(name, component) {
1818

1919
displayName: name,
2020

21-
getRoutes: function() {
22-
return this.props.children;
21+
getRoutes: function(props) {
22+
return props.children;
2323
},
2424

2525
getDefaultProps: function() {

lib/RouterMixin.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,32 @@ var RouterMixin = {
3636
},
3737

3838
getInitialState: function() {
39+
return this.getRouterState(this.props);
40+
},
41+
42+
componentWillReceiveProps: function(nextProps) {
43+
var nextState = this.getRouterState(nextProps);
44+
this.replaceState(nextState);
45+
},
46+
47+
getRouterState: function(props) {
3948
var path;
4049
var prefix;
4150

42-
if (this.props.contextual && this.context.router) {
51+
if (props.contextual && this.context.router) {
4352

4453
var match = this.context.router.getMatch();
4554

4655
invariant(
47-
this.props.path || isString(match.unmatchedPath),
56+
props.path || isString(match.unmatchedPath),
4857
"contextual router has nothing to match on: %s", match.unmatchedPath
4958
);
5059

51-
path = this.props.path || match.unmatchedPath;
60+
path = props.path || match.unmatchedPath;
5261
prefix = match.matchedPath;
5362
} else {
5463

55-
path = this.props.path || this.props.environment.getPath();
64+
path = props.path || props.environment.getPath();
5665

5766
invariant(
5867
isString(path),
@@ -68,16 +77,12 @@ var RouterMixin = {
6877
}
6978

7079
return {
71-
match: matchRoutes(this.getRoutes(), path),
80+
match: matchRoutes(this.getRoutes(props), path),
7281
prefix: prefix,
7382
navigation: {}
7483
};
7584
},
7685

77-
componentWillReceiveProps: function() {
78-
this.replaceState(this.getInitialState());
79-
},
80-
8186
/**
8287
* Return parent router or undefined.
8388
*/
@@ -126,7 +131,7 @@ var RouterMixin = {
126131
*/
127132
setPath: function(path, navigation, cb) {
128133
this.replaceState({
129-
match: matchRoutes(this.getRoutes(), path),
134+
match: matchRoutes(this.getRoutes(this.props), path),
130135
prefix: this.state.prefix,
131136
navigation: navigation
132137
}, cb);

0 commit comments

Comments
 (0)