@@ -81,20 +81,11 @@ export function useQueryStates<KeyMap extends UseQueryStatesKeysMap>(
81
81
Object . fromEntries (
82
82
Object . keys ( keyMap ) . map ( key => [ key , urlKeys [ key ] ?? key ] )
83
83
) ,
84
- [ stateKeys , urlKeys ]
84
+ [ stateKeys , JSON . stringify ( urlKeys ) ]
85
85
)
86
86
const adapter = useAdapter ( )
87
87
const initialSearchParams = adapter . searchParams
88
88
const queryRef = useRef < Record < string , string | null > > ( { } )
89
- // Initialise the queryRef with the initial values
90
- if ( Object . keys ( queryRef . current ) . length !== Object . keys ( keyMap ) . length ) {
91
- queryRef . current = Object . fromEntries (
92
- Object . values ( resolvedUrlKeys ) . map ( urlKey => [
93
- urlKey ,
94
- initialSearchParams ?. get ( urlKey ) ?? null
95
- ] )
96
- )
97
- }
98
89
const defaultValues = useMemo (
99
90
( ) =>
100
91
Object . fromEntries (
@@ -119,6 +110,29 @@ export function useQueryStates<KeyMap extends UseQueryStatesKeysMap>(
119
110
internalState ,
120
111
initialSearchParams
121
112
)
113
+ // Initialise the refs with the initial values
114
+ if (
115
+ Object . keys ( queryRef . current ) . join ( '&' ) !==
116
+ Object . values ( resolvedUrlKeys ) . join ( '&' )
117
+ ) {
118
+ const { state, hasChanged } = parseMap (
119
+ keyMap ,
120
+ urlKeys ,
121
+ initialSearchParams ,
122
+ queryRef . current ,
123
+ stateRef . current
124
+ )
125
+ if ( hasChanged ) {
126
+ stateRef . current = state
127
+ setInternalState ( state )
128
+ }
129
+ queryRef . current = Object . fromEntries (
130
+ Object . values ( resolvedUrlKeys ) . map ( urlKey => [
131
+ urlKey ,
132
+ initialSearchParams ?. get ( urlKey ) ?? null
133
+ ] )
134
+ )
135
+ }
122
136
123
137
useEffect ( ( ) => {
124
138
const { state, hasChanged } = parseMap (
0 commit comments