@@ -19,10 +19,12 @@ function hasAnyProperties(object) {
19
19
* Returns a new createHistory function that may be used to create
20
20
* history objects that know about routing.
21
21
*
22
- * - isActive(pathname, query)
23
- * - match(location, (error, nextState, nextLocation) => {})
24
- * - listenBeforeLeavingRoute(route, (?nextLocation) => {})
22
+ * Enhances history objects with the following methods:
23
+ *
25
24
* - listen((error, nextState) => {})
25
+ * - listenBeforeLeavingRoute(route, (nextLocation) => {})
26
+ * - match(location, (error, redirectLocation, nextState) => {})
27
+ * - isActive(pathname, query, indexOnly=false)
26
28
*/
27
29
function useRoutes ( createHistory ) {
28
30
return function ( options = { } ) {
@@ -34,13 +36,23 @@ function useRoutes(createHistory) {
34
36
return _isActive ( pathname , query , indexOnly , state . location , state . routes , state . params )
35
37
}
36
38
39
+ function createLocationFromRedirectInfo ( { pathname, query, state } ) {
40
+ return history . createLocation (
41
+ history . createPath ( pathname , query ) , state , REPLACE
42
+ )
43
+ }
44
+
37
45
let partialNextState
38
46
39
47
function match ( location , callback ) {
40
48
if ( partialNextState && partialNextState . location === location ) {
41
49
// Continue from where we left off.
42
50
finishMatch ( partialNextState , callback )
43
51
} else {
52
+ // Allow match(path)
53
+ if ( typeof location === 'string' )
54
+ location = history . createLocation ( location )
55
+
44
56
matchRoutes ( routes , location , function ( error , nextState ) {
45
57
if ( error ) {
46
58
callback ( error )
@@ -53,12 +65,6 @@ function useRoutes(createHistory) {
53
65
}
54
66
}
55
67
56
- function createLocationFromRedirectInfo ( { pathname, query, state } ) {
57
- return history . createLocation (
58
- history . createPath ( pathname , query ) , state , REPLACE , history . createKey ( )
59
- )
60
- }
61
-
62
68
function finishMatch ( nextState , callback ) {
63
69
let { leaveRoutes, enterRoutes } = computeChangedRoutes ( state , nextState )
64
70
@@ -75,7 +81,9 @@ function useRoutes(createHistory) {
75
81
if ( error ) {
76
82
callback ( error )
77
83
} else {
78
- callback ( null , null , { ...nextState , components } )
84
+ // TODO: Make match a pure function and have some other API
85
+ // for "match and update state".
86
+ callback ( null , null , ( state = { ...nextState , components } ) )
79
87
}
80
88
} )
81
89
}
@@ -221,18 +229,18 @@ function useRoutes(createHistory) {
221
229
if ( state . location === location ) {
222
230
listener ( null , state )
223
231
} else {
224
- match ( location , function ( error , nextLocation , nextState ) {
232
+ match ( location , function ( error , redirectLocation , nextState ) {
225
233
if ( error ) {
226
234
listener ( error )
227
- } else if ( nextLocation ) {
228
- history . transitionTo ( nextLocation )
235
+ } else if ( redirectLocation ) {
236
+ history . transitionTo ( redirectLocation )
229
237
} else if ( nextState ) {
230
- listener ( null , ( state = nextState ) )
238
+ listener ( null , nextState )
231
239
} else {
232
240
warning (
233
241
false ,
234
242
'Location "%s" did not match any routes' ,
235
- location . pathname + location . search
243
+ location . pathname + location . search + location . hash
236
244
)
237
245
}
238
246
} )
0 commit comments