From 75d5e453702cbd3dc1d2b428d54ab0a7d6eafc74 Mon Sep 17 00:00:00 2001 From: Khavin K K Date: Fri, 16 Aug 2024 20:54:17 -0400 Subject: [PATCH] fixed useParams not updating issue --- packages/wouter/src/index.js | 1 + packages/wouter/test/use-params.test.tsx | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/wouter/src/index.js b/packages/wouter/src/index.js index 80d415c..ebe13b6 100644 --- a/packages/wouter/src/index.js +++ b/packages/wouter/src/index.js @@ -198,6 +198,7 @@ const useCachedParams = (value) => { curr = prev.current; for (const k in value) if (value[k] !== curr[k]) curr = value; + if (Object.keys(value).length === 0) curr = value; return (prev.current = curr); }; diff --git a/packages/wouter/test/use-params.test.tsx b/packages/wouter/test/use-params.test.tsx index f0a2ea0..da31f27 100644 --- a/packages/wouter/test/use-params.test.tsx +++ b/packages/wouter/test/use-params.test.tsx @@ -1,6 +1,6 @@ import { act, renderHook } from "@testing-library/react"; import { it, expect } from "vitest"; -import { useParams, Router, Route } from "wouter"; +import { useParams, Router, Route, Switch } from "wouter"; import { memoryLocation } from "wouter/memory-location"; @@ -164,3 +164,22 @@ it("works when the route becomes matching", () => { act(() => navigate("/123")); expect(result.current).toMatchObject({ id: "123" }); }); + +it("makes the params an empty object, when there are no path params", () => { + const { hook, navigate } = memoryLocation({ path: "/" }); + + const { result } = renderHook(() => useParams(), { + wrapper: (props) => ( + + + {props.children} + {props.children} + + + ), + }); + + act(() => navigate("/posts/all")); + act(() => navigate("/posts")); + expect(Object.keys(result.current).length).toBe(0); +});