Skip to content

Commit

Permalink
feat: add parameter to regenerate the useMemo
Browse files Browse the repository at this point in the history
  • Loading branch information
MatiasWorker committed May 21, 2024
1 parent e2468cd commit 718bb92
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/use-router/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMemo, useState, createRef } from "atomico";
import { createMatch, Params } from "@uppercod/exp-route";
import { createMatch, Params, Match } from "@uppercod/exp-route";
import { useListener } from "@atomico/use-listener";
import { getPath, redirect } from "./history.js";
export * from "./history.js";
Expand All @@ -13,8 +13,11 @@ interface RouteSwitch<Result> {

const refGlobalThis = createRef(globalThis);

const cache: { [path: string]: Match } = {};

export function useRouter<Result = any>(
router: RouteSwitch<Result>,
memo?: any,
): {
id: string;
path: string;
Expand All @@ -30,11 +33,12 @@ export function useRouter<Result = any>(

return useMemo(() => {
for (const path in router) {
const params = createMatch(path)(id);
cache[path] = cache[path] || createMatch(path);
const params = cache[path](id);
if (params) {
const result = router[path](params, { id, path });
return { result, id, path, params, redirect };
}
}
}, [id]);
}, [id, memo]);
}

0 comments on commit 718bb92

Please sign in to comment.