-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
match.js
46 lines (40 loc) · 1.18 KB
/
match.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import invariant from 'invariant'
import createMemoryHistory from 'history/lib/createMemoryHistory'
import useBasename from 'history/lib/useBasename'
import { createRoutes } from './RouteUtils'
import useRoutes from './useRoutes'
const createHistory = useRoutes(useBasename(createMemoryHistory))
/**
* A high-level API to be used for server-side rendering.
*
* This function matches a location to a set of routes and calls
* callback(error, redirectLocation, renderProps) when finished.
*
* Note: You probably don't want to use this in a browser. Use
* the history.listen API instead.
*/
function match({
routes,
location,
parseQueryString,
stringifyQuery,
basename
}, callback) {
invariant(
location,
'match needs a location'
)
const history = createHistory({
routes: createRoutes(routes),
parseQueryString,
stringifyQuery,
basename
})
// Allow match({ location: '/the/path', ... })
if (typeof location === 'string')
location = history.createLocation(location)
history.match(location, function (error, redirectLocation, nextState) {
callback(error, redirectLocation, nextState && { ...nextState, history })
})
}
export default match