Skip to content

Commit 718bb92

Browse files
author
MatiasWorker
committed
feat: add parameter to regenerate the useMemo
1 parent e2468cd commit 718bb92

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

packages/use-router/src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useMemo, useState, createRef } from "atomico";
2-
import { createMatch, Params } from "@uppercod/exp-route";
2+
import { createMatch, Params, Match } from "@uppercod/exp-route";
33
import { useListener } from "@atomico/use-listener";
44
import { getPath, redirect } from "./history.js";
55
export * from "./history.js";
@@ -13,8 +13,11 @@ interface RouteSwitch<Result> {
1313

1414
const refGlobalThis = createRef(globalThis);
1515

16+
const cache: { [path: string]: Match } = {};
17+
1618
export function useRouter<Result = any>(
1719
router: RouteSwitch<Result>,
20+
memo?: any,
1821
): {
1922
id: string;
2023
path: string;
@@ -30,11 +33,12 @@ export function useRouter<Result = any>(
3033

3134
return useMemo(() => {
3235
for (const path in router) {
33-
const params = createMatch(path)(id);
36+
cache[path] = cache[path] || createMatch(path);
37+
const params = cache[path](id);
3438
if (params) {
3539
const result = router[path](params, { id, path });
3640
return { result, id, path, params, redirect };
3741
}
3842
}
39-
}, [id]);
43+
}, [id, memo]);
4044
}

0 commit comments

Comments
 (0)