1
1
import mitt from 'mitt'
2
2
import { startTransition , useCallback , useEffect , useState } from 'react'
3
3
import { renderQueryString } from '../../url-encoding'
4
+ import { createAdapterProvider } from './context'
4
5
import type { AdapterInterface , AdapterOptions } from './defs'
5
6
import {
7
+ patchHistory as applyHistoryPatch ,
6
8
historyUpdateMarker ,
7
- patchHistory ,
8
9
type SearchParamsSyncEmitter
9
10
} from './patch-history'
10
11
@@ -24,11 +25,17 @@ type UseSearchParams = (initial: URLSearchParams) => [URLSearchParams, {}]
24
25
25
26
// --
26
27
27
- export function createReactRouterBasedAdapter (
28
- adapter : string ,
29
- useNavigate : UseNavigate ,
28
+ type CreateReactRouterBasedAdapterArgs = {
29
+ adapter : string
30
+ useNavigate : UseNavigate
30
31
useSearchParams : UseSearchParams
31
- ) {
32
+ }
33
+
34
+ export function createReactRouterBasedAdapter ( {
35
+ adapter,
36
+ useNavigate,
37
+ useSearchParams
38
+ } : CreateReactRouterBasedAdapterArgs ) {
32
39
const emitter : SearchParamsSyncEmitter = mitt ( )
33
40
function useNuqsReactRouterBasedAdapter ( ) : AdapterInterface {
34
41
const navigate = useNavigate ( )
@@ -112,19 +119,18 @@ export function createReactRouterBasedAdapter(
112
119
return searchParams
113
120
}
114
121
/**
115
- * Opt-in to syncing shallow updates of the URL with the useOptimisticSearchParams hook.
122
+ * Sync shallow updates of the URL with the useOptimisticSearchParams hook.
116
123
*
117
124
* By default, the useOptimisticSearchParams hook will only react to internal nuqs updates.
118
125
* If third party code updates the History API directly, use this function to
119
126
* enable useOptimisticSearchParams to react to those changes.
127
+ *
128
+ * Note: this is actually required in React Router frameworks to follow Link navigations.
120
129
*/
121
- function enableHistorySync ( ) {
122
- patchHistory ( emitter , adapter )
123
- }
130
+ applyHistoryPatch ( emitter , adapter )
124
131
125
132
return {
126
- useNuqsReactRouterBasedAdapter,
127
- useOptimisticSearchParams,
128
- enableHistorySync
133
+ NuqsAdapter : createAdapterProvider ( useNuqsReactRouterBasedAdapter ) ,
134
+ useOptimisticSearchParams
129
135
}
130
136
}
0 commit comments