-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathuseQueryString.ts
More file actions
103 lines (92 loc) · 3.31 KB
/
Copy pathuseQueryString.ts
File metadata and controls
103 lines (92 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { useNavigate } from 'react-router-dom';
/**
* A hook that leverages React Router v6 to sync URL params with the React component lifecycle.
* You can use this hook to conditionally render tabs, forms, or other screens based on the current URL parameters.
*
* For instance, `/my-profile?tab=Stats`:
* - Calling this hook returns `queryString`, which is an object containing the key `tab` with the value `Stats`.
* - You can then conditionally render the `Stats` tab screen by checking if `queryString.tab === 'Stats'`.
*
* This avoids the need for prop drilling to pass boolean screen setters into child components for switching between different screens/tabs.
*
* @example
* // Call the hook and render the tab based on `?tab=...`
* const { queryString } = useQueryString();
*
* if (queryString.tab === 'Stats') {
* // Show Stats component
* }
*
* @returns {object} An object containing:
* - `deleteQueryString`: A function to remove a specific query parameter from the URL.
* - `queryString`: An object representing the current URL query parameters.
* - `setQueryString`: A function to add or update query parameters in the URL.
*
* @example
* // Deleting a query parameter
* const { deleteQueryString } = useQueryString();
* deleteQueryString('tab'); // Removes the 'tab' query parameter from the URL
*
* @example
* // Setting a query parameter
* const { setQueryString } = useQueryString();
* setQueryString({ tab: 'Payment methods', modal: 'NicknameModal' });
* // Updates the URL to include `tab=Payment+methods&modal=NicknameModal`
*/
interface QueryParams {
advertId: string;
formAction: string;
modal: string;
paymentMethodId: string;
tab: string;
}
function useQueryString() {
const navigate = useNavigate();
// Parse the query string into an object
function getQueryParams(): Partial<QueryParams> {
const searchParams = new URLSearchParams(window.location.search);
const params: Partial<QueryParams> = {};
searchParams.forEach((value, key) => {
params[key as keyof QueryParams] = value;
});
return params;
}
// Update the query string in the URL
function setQueryParams(newParams: Partial<QueryParams>) {
const searchParams = new URLSearchParams(window.location.search);
Object.entries(newParams).forEach(([key, value]) => {
if (value === undefined) {
searchParams.delete(key);
} else {
searchParams.set(key, value);
}
});
navigate(
{
search: searchParams.toString(),
},
{ replace: true }
);
}
// Function to delete a specific query parameter
function deleteQueryString(key: keyof QueryParams) {
const searchParams = new URLSearchParams(window.location.search);
searchParams.delete(key);
navigate(
{
search: searchParams.toString(),
},
{ replace: true }
);
}
// Function to set multiple query parameters at once
function setQueryString(queryStrings: Partial<QueryParams>) {
setQueryParams(queryStrings);
}
return {
deleteQueryString,
queryString: getQueryParams(),
setQueryString,
};
}
export default useQueryString;