Skip to content

Commit

Permalink
Add useQueryParams hook for managing query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
MuttakinHasib committed Feb 17, 2024
1 parent 254c3d1 commit 299422a
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions apps/web/src/hooks/useQueryParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { buildQuery, getQueries } from "@/utils";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useCallback } from "react";

export const useQueryParams = () => {
const searchParams = useSearchParams();
const pathname = usePathname();
const { replace } = useRouter();

const setQuery = useCallback(
<T = Record<string, string | string[]>>(queries: T) => {
const current = getQueries(searchParams);
Object.assign(current, queries);
const query = buildQuery(current);
replace(`${pathname}?${query}`);
},
[pathname, replace, searchParams]
);

return { setQuery };
};

0 comments on commit 299422a

Please sign in to comment.