From 718bb92cf7ea1309415bdcfd4415699013e41b38 Mon Sep 17 00:00:00 2001 From: MatiasWorker Date: Tue, 21 May 2024 18:02:09 -0400 Subject: [PATCH] feat: add parameter to regenerate the useMemo --- packages/use-router/src/index.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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]); }