Skip to content

Commit

Permalink
Preserve query params on top navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
eyezick committed Jul 22, 2024
1 parent f293696 commit 8f5df63
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/components/NavLink.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Button } from "flowbite-react";
import { useLocation } from "wouter";
import { useSearchParams } from "#/hooks/searchParams";

export default function NavLink({ href, children, useButton = false, ...opts }) {
const LinkComponent = useButton ? Button : "a";
const [_location, navigate] = useLocation();
const [, , navigate] = useSearchParams();
const goTo = (event, ref) => {
if (navigate) {
event.preventDefault();
Expand Down
10 changes: 9 additions & 1 deletion src/hooks/searchParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@ export function useSearchParams() {
[location, navigate],
);

return [searchParams, setSearchParams] as const;
const handleNavigate = useCallback(
(href: string, preserveParams = true) => {
const params = preserveParams ? `?${search}` : "";
navigate(`${href}${params}`);
},
[navigate, search],
);

return [searchParams, setSearchParams, handleNavigate] as const;
}

0 comments on commit 8f5df63

Please sign in to comment.