@@ -3,7 +3,7 @@ import React from 'react'
3
3
import { debug } from './debug'
4
4
import type { Options } from './defs'
5
5
import type { Parser } from './parsers'
6
- import { SYNC_EVENT_KEY , emitter } from './sync'
6
+ import { SYNC_EVENT_KEY , emitter , type CrossHookSyncPayload } from './sync'
7
7
import {
8
8
FLUSH_RATE_LIMIT_MS ,
9
9
enqueueQueryStringUpdate ,
@@ -225,12 +225,12 @@ export function useQueryState<T = string>(
225
225
const router = useRouter ( )
226
226
// Not reactive, but available on the server and on page load
227
227
const initialSearchParams = useSearchParams ( )
228
- const valueRef = React . useRef < string | null > ( null )
228
+ const queryRef = React . useRef < string | null > ( null )
229
229
const [ internalState , setInternalState ] = React . useState < T | null > ( ( ) => {
230
230
const queueValue = getQueuedValue ( key )
231
231
const urlValue = initialSearchParams ?. get ( key ) ?? null
232
232
const value = queueValue ?? urlValue
233
- valueRef . current = value
233
+ queryRef . current = value
234
234
return value === null ? null : safeParse ( parse , value , key )
235
235
} )
236
236
const stateRef = React . useRef ( internalState )
@@ -247,34 +247,33 @@ export function useQueryState<T = string>(
247
247
if ( window . next ?. version !== '14.0.3' ) {
248
248
return
249
249
}
250
- const value = initialSearchParams . get ( key ) ?? null
251
- if ( value === valueRef . current ) {
250
+ const query = initialSearchParams . get ( key ) ?? null
251
+ if ( query === queryRef . current ) {
252
252
return
253
253
}
254
- const state = value === null ? null : safeParse ( parse , value , key )
254
+ const state = query === null ? null : safeParse ( parse , query , key )
255
255
debug ( '[nuqs `%s`] syncFromUseSearchParams %O' , key , state )
256
256
stateRef . current = state
257
- valueRef . current = value
257
+ queryRef . current = query
258
258
setInternalState ( state )
259
259
} , [ initialSearchParams ?. get ( key ) , key ] )
260
260
261
261
// Sync all hooks together & with external URL changes
262
262
React . useInsertionEffect ( ( ) => {
263
- function updateInternalState ( state : T | null ) {
263
+ function updateInternalState ( { state, query } : CrossHookSyncPayload ) {
264
264
debug ( '[nuqs `%s`] updateInternalState %O' , key , state )
265
265
stateRef . current = state
266
- valueRef . current = state === null ? null : serialize ( state )
266
+ queryRef . current = query
267
267
setInternalState ( state )
268
268
}
269
269
function syncFromURL ( search : URLSearchParams ) {
270
- const value = search . get ( key ) ?? null
271
- if ( value === valueRef . current ) {
270
+ const query = search . get ( key )
271
+ if ( query === queryRef . current ) {
272
272
return
273
273
}
274
- const state = value === null ? null : safeParse ( parse , value , key )
274
+ const state = query === null ? null : safeParse ( parse , query , key )
275
275
debug ( '[nuqs `%s`] syncFromURL %O' , key , state )
276
- updateInternalState ( state )
277
- valueRef . current = value
276
+ updateInternalState ( { state, query } )
278
277
}
279
278
debug ( '[nuqs `%s`] subscribing to sync' , key )
280
279
emitter . on ( SYNC_EVENT_KEY , syncFromURL )
@@ -299,16 +298,16 @@ export function useQueryState<T = string>(
299
298
) {
300
299
newValue = null
301
300
}
302
- // Sync all hooks state (including this one)
303
- emitter . emit ( key , newValue )
304
- valueRef . current = enqueueQueryStringUpdate ( key , newValue , serialize , {
301
+ queryRef . current = enqueueQueryStringUpdate ( key , newValue , serialize , {
305
302
// Call-level options take precedence over hook declaration options.
306
303
history : options . history ?? history ,
307
304
shallow : options . shallow ?? shallow ,
308
305
scroll : options . scroll ?? scroll ,
309
306
throttleMs : options . throttleMs ?? throttleMs ,
310
307
startTransition : options . startTransition ?? startTransition
311
308
} )
309
+ // Sync all hooks state (including this one)
310
+ emitter . emit ( key , { state : newValue , query : queryRef . current } )
312
311
return scheduleFlushToURL ( router )
313
312
} ,
314
313
[ key , history , shallow , scroll , throttleMs , startTransition ]
0 commit comments