Skip to content

Commit 0c3c75d

Browse files
committed
Merge pull request #499 from vladimir-vg/will-transition-to-query-fix
[fixed] Now execute willTransition* hooks even if only query part was changed
2 parents 6417285 + c6aa4d3 commit 0c3c75d

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

modules/__tests__/Router-test.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,46 @@ describe('Router', function () {
532532
done();
533533
});
534534
});
535+
536+
it('execute willTransition* callbacks when query changes', function (done) {
537+
var fromCallbackExecuted = false;
538+
var Spoon = React.createClass({
539+
statics: {
540+
willTransitionTo: function (transition, params, query) {
541+
if (query['filter'] === 'first') {
542+
return; // skip first transition
543+
}
544+
545+
expect(query['filter']).toEqual('second');
546+
expect(fromCallbackExecuted).toBe(true);
547+
done();
548+
},
549+
550+
willTransitionFrom: function (transition, element) {
551+
fromCallbackExecuted = true;
552+
}
553+
},
554+
555+
render: function () {
556+
return <h1>Spoon</h1>;
557+
}
558+
});
559+
560+
var routes = (
561+
<Route handler={Nested} path='/'>
562+
<Route name="spoon" handler={Spoon}/>
563+
</Route>
564+
);
565+
566+
TestLocation.history = [ '/spoon?filter=first' ];
567+
568+
var div = document.createElement('div');
569+
Router.run(routes, TestLocation, function (Handler, state) {
570+
React.render(<Handler/>, div);
571+
});
572+
573+
TestLocation.push('/spoon?filter=second');
574+
});
535575
});
536576

537577
describe('willTransitionFrom', function () {
@@ -566,7 +606,6 @@ describe('Router', function () {
566606
});
567607
});
568608
});
569-
570609
});
571610

572611
});

modules/utils/createRouter.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,16 @@ function createRouter(options) {
334334
toRoutes = nextRoutes;
335335
}
336336

337+
// If routes' hooks arrays are empty, then we transition to current route.
338+
// But path is somehow still get changed.
339+
// That could be only because of route query changes.
340+
// Need to push current route to routes' hooks arrays.
341+
if (!toRoutes.length && !fromRoutes.length) {
342+
var currentRoute = state.routes[state.routes.length-1];
343+
fromRoutes = [currentRoute];
344+
toRoutes = [currentRoute];
345+
}
346+
337347
var transition = new Transition(path, this.replaceWith.bind(this, path));
338348
pendingTransition = transition;
339349

0 commit comments

Comments
 (0)