Skip to content

Commit

Permalink
fixed useParams not updating issue
Browse files Browse the repository at this point in the history
  • Loading branch information
khavin authored and molefrog committed Aug 21, 2024
1 parent 3d1f732 commit 75d5e45
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/wouter/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
21 changes: 20 additions & 1 deletion packages/wouter/test/use-params.test.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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) => (
<Router hook={hook}>
<Switch>
<Route path="/posts">{props.children}</Route>
<Route path="/posts/:a">{props.children}</Route>
</Switch>
</Router>
),
});

act(() => navigate("/posts/all"));
act(() => navigate("/posts"));
expect(Object.keys(result.current).length).toBe(0);
});

0 comments on commit 75d5e45

Please sign in to comment.