Skip to content

Commit b9079c9

Browse files
committed
[added] getPathname to Router.State
Fixes remix-run#413
1 parent c7c1fb5 commit b9079c9

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

modules/mixins/Scrolling.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
var invariant = require('react/lib/invariant');
22
var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM;
33
var getWindowScrollPosition = require('../utils/getWindowScrollPosition');
4-
var Path = require('../utils/Path');
54

65
function shouldUpdateScroll(state, prevState) {
7-
if (!prevState) {
6+
if (!prevState)
87
return true;
9-
}
108

11-
var path = state.path;
9+
// Don't update scroll position when only the query has changed.
10+
if (state.pathname === prevState.pathname)
11+
return false;
12+
1213
var routes = state.routes;
13-
var prevPath = prevState.path;
1414
var prevRoutes = prevState.routes;
1515

16-
if (Path.withoutQuery(path) === Path.withoutQuery(prevPath)) {
17-
return false;
18-
}
19-
2016
var sharedAncestorRoutes = routes.filter(function (route) {
2117
return prevRoutes.indexOf(route) !== -1;
2218
});

modules/mixins/State.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var State = {
2323
contextTypes: {
2424
getCurrentPath: React.PropTypes.func.isRequired,
2525
getCurrentRoutes: React.PropTypes.func.isRequired,
26+
getCurrentPathname: React.PropTypes.func.isRequired,
2627
getCurrentParams: React.PropTypes.func.isRequired,
2728
getCurrentQuery: React.PropTypes.func.isRequired,
2829
isActive: React.PropTypes.func.isRequired
@@ -42,6 +43,13 @@ var State = {
4243
return this.context.getCurrentRoutes();
4344
},
4445

46+
/**
47+
* Returns the current URL path without the query string.
48+
*/
49+
getPathname: function () {
50+
return this.context.getCurrentPathname();
51+
},
52+
4553
/**
4654
* Returns an object of the URL params that are currently active.
4755
*/

modules/mixins/StateContext.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ var StateContext = {
4343
return this.state.routes.slice(0);
4444
},
4545

46+
/**
47+
* Returns the current URL path without the query string.
48+
*/
49+
getCurrentPathname: function () {
50+
return this.state.pathname;
51+
},
52+
4653
/**
4754
* Returns a read-only object of the currently active URL parameters.
4855
*/
@@ -72,6 +79,7 @@ var StateContext = {
7279
childContextTypes: {
7380
getCurrentPath: React.PropTypes.func.isRequired,
7481
getCurrentRoutes: React.PropTypes.func.isRequired,
82+
getCurrentPathname: React.PropTypes.func.isRequired,
7583
getCurrentParams: React.PropTypes.func.isRequired,
7684
getCurrentQuery: React.PropTypes.func.isRequired,
7785
isActive: React.PropTypes.func.isRequired
@@ -81,6 +89,7 @@ var StateContext = {
8189
return {
8290
getCurrentPath: this.getCurrentPath,
8391
getCurrentRoutes: this.getCurrentRoutes,
92+
getCurrentPathname: this.getCurrentPathname,
8493
getCurrentParams: this.getCurrentParams,
8594
getCurrentQuery: this.getCurrentQuery,
8695
isActive: this.isActive

modules/utils/createRouter.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ function createRouter(options) {
255255
* Performs a match of the given pathname against this router and returns an object
256256
* with the { routes, params } that match. Returns null if no match can be made.
257257
*/
258-
match: function (path) {
259-
return findMatch(Path.withoutQuery(path), routes, this.defaultRoute, this.notFoundRoute) || null;
258+
match: function (pathname) {
259+
return findMatch(pathname, routes, this.defaultRoute, this.notFoundRoute) || null;
260260
},
261261

262262
/**
@@ -289,7 +289,8 @@ function createRouter(options) {
289289
this.recordScrollPosition(prevPath);
290290
}
291291

292-
var match = this.match(path);
292+
var pathname = Path.withoutQuery(path);
293+
var match = this.match(pathname);
293294

294295
warning(
295296
match != null,
@@ -334,6 +335,7 @@ function createRouter(options) {
334335

335336
nextState.path = path;
336337
nextState.action = action;
338+
nextState.pathname = pathname;
337339
nextState.routes = nextRoutes;
338340
nextState.params = nextParams;
339341
nextState.query = nextQuery;

0 commit comments

Comments
 (0)