Skip to content

Commit 16fff9b

Browse files
committed
Emit warning when no routes match
1 parent a9f9f3c commit 16fff9b

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

modules/useRoutes.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warning from 'warning';
12
import useQueries from 'history/lib/useQueries';
23
import computeChangedRoutes from './computeChangedRoutes';
34
import { runEnterHooks, runLeaveHooks } from './TransitionUtils';
@@ -24,6 +25,20 @@ function useRoutes(createHistory) {
2425
return _isActive(pathname, query, state.location, state.routes, state.params);
2526
}
2627

28+
function matchRoutesWithWarning(routes, location, callback) {
29+
matchRoutes(routes, location, function (error, nextState) {
30+
if (error || nextState) {
31+
callback(error, nextState);
32+
} else {
33+
warning(
34+
false,
35+
'Location "%s" did not match any routes',
36+
location.pathname + location.search
37+
);
38+
}
39+
});
40+
}
41+
2742
// TODO: If we had a way to uniquely identify a route,
2843
// we could use a plain object here instead...
2944
var routeHooks = new Map();
@@ -34,7 +49,7 @@ function useRoutes(createHistory) {
3449
// Continue from where we left off.
3550
finishMatch(partialNextState, callback);
3651
} else {
37-
matchRoutes(routes, location, function (error, nextState) {
52+
matchRoutesWithWarning(routes, location, function (error, nextState) {
3853
if (error) {
3954
callback(error);
4055
} else {
@@ -78,7 +93,7 @@ function useRoutes(createHistory) {
7893
}
7994

8095
function transitionHook(location, callback) {
81-
matchRoutes(routes, location, function (error, nextState) {
96+
matchRoutesWithWarning(routes, location, function (error, nextState) {
8297
if (error) {
8398
// TODO: Handle the error.
8499
callback(false); // Cancel the transition.

0 commit comments

Comments
 (0)