@@ -3,6 +3,7 @@ import { startTransition, useCallback, useEffect, useState } from 'react'
3
3
import { renderQueryString } from '../../url-encoding'
4
4
import { createAdapterProvider } from './context'
5
5
import type { AdapterInterface , AdapterOptions } from './defs'
6
+ import { applyChange , filterSearchParams } from './key-isolation'
6
7
import {
7
8
patchHistory as applyHistoryPatch ,
8
9
historyUpdateMarker ,
@@ -37,9 +38,11 @@ export function createReactRouterBasedAdapter({
37
38
useSearchParams
38
39
} : CreateReactRouterBasedAdapterArgs ) {
39
40
const emitter : SearchParamsSyncEmitter = mitt ( )
40
- function useNuqsReactRouterBasedAdapter ( ) : AdapterInterface {
41
+ function useNuqsReactRouterBasedAdapter (
42
+ watchKeys : string [ ]
43
+ ) : AdapterInterface {
41
44
const navigate = useNavigate ( )
42
- const searchParams = useOptimisticSearchParams ( )
45
+ const searchParams = useOptimisticSearchParams ( watchKeys )
43
46
const updateUrl = useCallback (
44
47
( search : URLSearchParams , options : AdapterOptions ) => {
45
48
startTransition ( ( ) => {
@@ -83,7 +86,7 @@ export function createReactRouterBasedAdapter({
83
86
updateUrl
84
87
}
85
88
}
86
- function useOptimisticSearchParams ( ) {
89
+ function useOptimisticSearchParams ( watchKeys : string [ ] = [ ] ) {
87
90
const [ serverSearchParams ] = useSearchParams (
88
91
// Note: this will only be taken into account the first time the hook is called,
89
92
// and cached for subsequent calls, causing problems when mounting components
@@ -93,21 +96,26 @@ export function createReactRouterBasedAdapter({
93
96
: new URLSearchParams ( location . search )
94
97
)
95
98
const [ searchParams , setSearchParams ] = useState ( ( ) => {
96
- if ( typeof location === 'undefined' ) {
97
- // We use this on the server to SSR with the correct search params.
98
- return serverSearchParams
99
- }
100
- // Since useSearchParams isn't reactive to shallow changes,
101
- // it doesn't pick up changes in the URL on mount, so we need to initialise
102
- // the reactive state with the current URL instead.
103
- return new URLSearchParams ( location . search )
99
+ return typeof location === 'undefined'
100
+ ? // We use this on the server to SSR with the correct search params.
101
+ filterSearchParams ( serverSearchParams , watchKeys , true )
102
+ : // Since useSearchParams isn't reactive to shallow changes,
103
+ // it doesn't pick up changes in the URL on mount, so we need to initialise
104
+ // the reactive state with the current URL instead.
105
+ filterSearchParams (
106
+ new URLSearchParams ( location . search ) ,
107
+ watchKeys ,
108
+ false // No need for a copy here
109
+ )
104
110
} )
105
111
useEffect ( ( ) => {
106
112
function onPopState ( ) {
107
- setSearchParams ( new URLSearchParams ( location . search ) )
113
+ setSearchParams (
114
+ applyChange ( new URLSearchParams ( location . search ) , watchKeys , false )
115
+ )
108
116
}
109
117
function onEmitterUpdate ( search : URLSearchParams ) {
110
- setSearchParams ( search )
118
+ setSearchParams ( applyChange ( search , watchKeys , true ) )
111
119
}
112
120
emitter . on ( 'update' , onEmitterUpdate )
113
121
window . addEventListener ( 'popstate' , onPopState )
0 commit comments