diff --git a/packages/wouter/src/index.js b/packages/wouter/src/index.js index 8a20f8b..fc13290 100644 --- a/packages/wouter/src/index.js +++ b/packages/wouter/src/index.js @@ -265,7 +265,9 @@ export const Link = forwardRef((props, ref) => { // handle nested routers and absolute paths const href = router.hrefs( - targetPath[0] === "~" ? targetPath.slice(1) : router.base + targetPath, + (targetPath ?? "")[0] === "~" + ? targetPath.slice(1) + : router.base + targetPath, router // pass router as a second argument for convinience ); diff --git a/packages/wouter/test/link.test.tsx b/packages/wouter/test/link.test.tsx index 5208024..204027d 100644 --- a/packages/wouter/test/link.test.tsx +++ b/packages/wouter/test/link.test.tsx @@ -322,4 +322,17 @@ describe(" with `asChild` prop", () => { expect(link).toHaveAttribute("href", "/about"); expect(link).toHaveTextContent("Click Me"); }); + + it("missing href or to won't crash", () => { + const { getByText } = render( + /* @ts-expect-error */ + Click Me + ); + + const link = getByText("Click Me"); + + expect(link.tagName).toBe("A"); + expect(link).toHaveAttribute("href", undefined); + expect(link).toHaveTextContent("Click Me"); + }); });