Skip to content

Commit 269b47a

Browse files
authored
Merge pull request #2 from nabepon/on_async_loader
feat: add option to detect if route was changed
2 parents 46bc817 + b559cde commit 269b47a

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ explicitly if you want to use store's state.
124124
If you want to invoke `asyncLoader()` when just querystring (not path) is changed, you must specify key names of querystring to router.
125125

126126
```
127-
<Route path="items" component=ItemList queryKeys="q, page" ... />
127+
<Route path="items" component=ItemList asyncLoaderProps={{queryKeys: "q, page"}} ... />
128128
```
129129

130130
Or, you can use the wildcard for any keys of querystring:
131131

132132
```
133-
<Route path="items" component=ItemList queryKeys="*" ... />
133+
<Route path="items" component=ItemList asyncLoaderProps={{queryKeys: "*"}} ... />
134134
```
135135

136136
### 5. Async data loading by mounting/updating (client-side rendering only)

src/computeChangedRoutes.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export default function computeChangedRoutes(prevState, nextState) {
1919
const leaveIndex = prevRoutes.findIndex((route) => (
2020
nextRoutes.indexOf(route) === -1 ||
2121
routeParamsChanged(route, prevState, nextState) ||
22-
queryParamsChanged(route, prevState, nextState)
22+
queryParamsChanged(route, prevState, nextState) ||
23+
routeChanged(route, prevState, nextState)
2324
));
2425
const leaveRoutes = leaveIndex === -1 ? [] : prevRoutes.slice(leaveIndex);
2526

@@ -44,7 +45,7 @@ function routeParamsChanged(route, prevState, nextState) {
4445
}
4546

4647
function queryParamsChanged(route, prevState, nextState) {
47-
const queryKeys = route.queryKeys;
48+
const queryKeys = route.asyncLoaderProps && route.asyncLoaderProps.queryKeys || route.queryKeys;
4849
if (!queryKeys) {
4950
return false;
5051
}
@@ -63,3 +64,8 @@ function queryParamsChanged(route, prevState, nextState) {
6364
const keys = queryKeys.split(/[, ]+/);
6465
return keys.some((key) => prevQuery[key] !== nextQuery[key]);
6566
}
67+
68+
function routeChanged(route, prevState, nextState) {
69+
const routeChanged = route.asyncLoaderProps && route.asyncLoaderProps.routeChanged;
70+
return routeChanged && routeChanged(route, prevState, nextState);
71+
}

0 commit comments

Comments
 (0)