forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseRoutes.js
More file actions
30 lines (28 loc) · 890 Bytes
/
useRoutes.js
File metadata and controls
30 lines (28 loc) · 890 Bytes
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
import warning from 'warning';
import matchRoutes from './matchRoutes';
export default function useRoutes(routes) {
return match => (prevState, location, callback) => {
matchRoutes(routes, location, (error1, state = { routes: [], params: {} }) => {
// State from `matchRoutes()` is *not* complete router state
// Only has `routes` (active routes) and `params`
if (error1) {
callback(error1);
return;
}
if (!routes.length) {
warning(
false,
'Location "%s" did not match any routes',
location.pathname + location.search
);
}
match({ ...prevState, ...state }, location, (error2, nextState, redirectInfo) => {
if (error2 || redirectInfo) {
callback(error2, null, redirectInfo);
return;
}
callback(null, nextState);
})
});
}
}