1 import { useSearchParams } from "react-router-dom";
2
3 type QueryParams<T extends string> = {
4 [key in T]: string | undefined;
5 };
6
7 export function useQueryParams<T extends string>(
8 keys: T[],
9 prefix?: string
10 ): QueryParams<T> {
11 const [params] = useSearchParams();
12
13 // Use a type assertion to initialize the result
14 const result = {} as QueryParams<T>;
15
16 keys.forEach((key) => {
17 const prefixedKey = prefix ? `${prefix}_${key}` : key;
18 const value = params.get(prefixedKey) || undefined;
19
20 result[key] = value;
21 });
22
23 return result;
24 }