@@ -3,13 +3,30 @@ import { ParsedQuery, parse, stringify } from 'query-string'
3
3
import { useCallback , useMemo } from 'react'
4
4
import { useHistory , useLocation } from 'react-router-dom'
5
5
6
+ interface Options {
7
+ /** Set to true to push a new entry onto the history stack */
8
+ push : boolean
9
+ }
10
+
6
11
const useQueryParams = ( ) : {
7
12
queryParams : ParsedQuery < string | number | boolean > ;
8
- replaceQueryParams : ( newParams : Record < string , unknown > ) => void ;
9
- setQueryParams : ( nextParams : Record < string , unknown > ) => void ;
13
+ /**
14
+ * Replace the query params in the url. It erase all current values and put the new ones
15
+ *
16
+ * @param newParams - The values to set in the query string, overweriting existing one
17
+ * @param options - Options to define behavior
18
+ */
19
+ replaceQueryParams : typeof replaceQueryParams
20
+ /**
21
+ * Set query params in the url. It merge the existing values with the new ones.
22
+ *
23
+ * @param nextParams - The values to add or update in the existing query string
24
+ * @param options - Options to define behavior
25
+ */
26
+ setQueryParams : typeof setQueryParams
10
27
} => {
11
28
// eslint-disable-next-line @typescript-eslint/unbound-method
12
- const { replace } = useHistory < History > ( )
29
+ const { replace, push } = useHistory < History > ( )
13
30
// eslint-disable-next-line @typescript-eslint/unbound-method
14
31
const location = useLocation < History > ( )
15
32
@@ -35,35 +52,28 @@ const useQueryParams = (): {
35
52
)
36
53
37
54
const replaceInUrlIfNeeded = useCallback (
38
- newState => {
55
+ ( newState : Record < string , unknown > , options ?: Options ) => {
39
56
const stringifiedParams = stringyFormat ( newState )
40
57
const searchToCompare = location . search || '?'
41
58
42
59
if ( searchToCompare !== `?${ stringifiedParams } ` ) {
43
- replace ( `${ location . pathname } ?${ stringifiedParams } ` )
60
+ const fn = options ?. push ? push : replace
61
+ fn ( `${ location . pathname } ?${ stringifiedParams } ` )
44
62
}
45
63
} ,
46
- [ replace , location . pathname , location . search , stringyFormat ] ,
64
+ [ push , replace , location . pathname , location . search , stringyFormat ] ,
47
65
)
48
66
49
- /**
50
- * Set query params in the url. It merge the existing values with the new ones.
51
- * @param {Object } nextParams The params to set in the url as query params
52
- */
53
67
const setQueryParams = useCallback (
54
- ( nextParams : Record < string , unknown > ) : void => {
55
- replaceInUrlIfNeeded ( { ...currentState , ...nextParams } )
68
+ ( nextParams : Record < string , unknown > , options ?: Options ) : void => {
69
+ replaceInUrlIfNeeded ( { ...currentState , ...nextParams } , options )
56
70
} ,
57
71
[ currentState , replaceInUrlIfNeeded ] ,
58
72
)
59
73
60
- /**
61
- * Replace the query params in the url. It erase all current values and put the new ones
62
- * @param {Object } newParams
63
- */
64
74
const replaceQueryParams = useCallback (
65
- ( newParams : Record < string , unknown > ) : void => {
66
- replaceInUrlIfNeeded ( { ... newParams } )
75
+ ( newParams : Record < string , unknown > , options ?: Options ) : void => {
76
+ replaceInUrlIfNeeded ( newParams , options )
67
77
} ,
68
78
[ replaceInUrlIfNeeded ] ,
69
79
)
0 commit comments