@@ -5,6 +5,7 @@ type QueryParamType = 'string' | 'string[]' | 'number' | 'number[]' | 'boolean'
5
5
6
6
export const queryRef = < T > ( key : string , defaultValue : T | null = null ) => {
7
7
const type = getType ( defaultValue )
8
+ const fixedDefaultValue = JSON . parse ( JSON . stringify ( defaultValue ) )
8
9
9
10
const loadedValue = loadQueryParamFromURL ( key , type )
10
11
@@ -13,7 +14,7 @@ export const queryRef = <T>(key: string, defaultValue: T | null = null) => {
13
14
watch (
14
15
queryRef ,
15
16
async ( newVal ) => {
16
- updateQueryParamInURL ( key , newVal , defaultValue , type )
17
+ updateQueryParamInURL ( key , newVal , fixedDefaultValue , type )
17
18
} ,
18
19
{ deep : true } ,
19
20
)
@@ -24,8 +25,8 @@ export const queryRef = <T>(key: string, defaultValue: T | null = null) => {
24
25
function updateQueryParamInURL ( key , value , defaultValue , type : QueryParamType ) {
25
26
if ( typeof window == 'undefined' ) return
26
27
27
- if ( [ 'string[]' , 'number[]' , 'boolean[]' ] . includes ( type ) ) value = value . join ( ',' )
28
- if ( [ 'object' , 'object[]' ] . includes ( type ) ) value = JSON . stringify ( value )
28
+ value = valueToString ( value , type )
29
+ defaultValue = valueToString ( defaultValue , type )
29
30
30
31
const url = new URL ( window . location . href )
31
32
if ( value != defaultValue ) {
@@ -57,3 +58,10 @@ function getType(defaultValue: any): QueryParamType {
57
58
58
59
return _type
59
60
}
61
+
62
+ function valueToString ( value , type ) : string {
63
+ if ( [ 'string[]' , 'number[]' , 'boolean[]' ] . includes ( type ) ) value = value . join ( ',' )
64
+ if ( [ 'object' , 'object[]' ] . includes ( type ) ) value = JSON . stringify ( value )
65
+
66
+ return value
67
+ }
0 commit comments