diff --git a/packages/use-router/src/index.ts b/packages/use-router/src/index.ts index c6b33e3..9efd6a6 100644 --- a/packages/use-router/src/index.ts +++ b/packages/use-router/src/index.ts @@ -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"; @@ -13,8 +13,11 @@ interface RouteSwitch { const refGlobalThis = createRef(globalThis); +const cache: { [path: string]: Match } = {}; + export function useRouter( router: RouteSwitch, + memo?: any, ): { id: string; path: string; @@ -30,11 +33,12 @@ export function useRouter( 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]); }