@@ -4,15 +4,29 @@ import {
44 useMatches ,
55 useNavigate
66} from '@tanstack/react-router'
7- import { startTransition , useCallback , useMemo } from 'react'
7+ import {
8+ createContext ,
9+ createElement ,
10+ startTransition ,
11+ useCallback ,
12+ useContext ,
13+ useMemo ,
14+ type ReactElement ,
15+ type ReactNode
16+ } from 'react'
817import { renderQueryString } from '../lib/url-encoding'
9- import { createAdapterProvider , type AdapterProvider } from './lib/context'
18+ import { createAdapterProvider , type AdapterProps } from './lib/context'
1019import type { AdapterInterface , UpdateUrlFunction } from './lib/defs'
1120
1221// Use TanStack Router's default JSON-based search param serialization
1322const defaultStringifySearch = stringifySearchWith ( JSON . stringify )
1423
24+ const NuqsTanstackRouterAdapterContext = createContext ( {
25+ stringifySearchWith : defaultStringifySearch
26+ } )
27+
1528function useNuqsTanstackRouterAdapter ( watchKeys : string [ ] ) : AdapterInterface {
29+ const { stringifySearchWith } = useContext ( NuqsTanstackRouterAdapterContext )
1630 const search = useLocation ( {
1731 select : state =>
1832 Object . fromEntries (
@@ -28,11 +42,11 @@ function useNuqsTanstackRouterAdapter(watchKeys: string[]): AdapterInterface {
2842 } )
2943 const searchParams = useMemo (
3044 ( ) =>
31- // Use TSR’s default stringify to convert search object → URLSearchParams.
45+ // Use TSR's stringify to convert search object → URLSearchParams.
3246 // This avoids issues where arrays/objects were previously flattened
3347 // into invalid values like "[object Object]".
34- new URLSearchParams ( defaultStringifySearch ( search ) ) ,
35- [ search , watchKeys . join ( ',' ) ]
48+ new URLSearchParams ( stringifySearchWith ( search ) ) ,
49+ [ search , stringifySearchWith , watchKeys . join ( ',' ) ]
3650 )
3751
3852 const updateUrl : UpdateUrlFunction = useCallback (
@@ -72,6 +86,21 @@ function useNuqsTanstackRouterAdapter(watchKeys: string[]): AdapterInterface {
7286 }
7387}
7488
75- export const NuqsAdapter : AdapterProvider = createAdapterProvider (
89+ const NuqsTanstackRouterAdapter = createAdapterProvider (
7690 useNuqsTanstackRouterAdapter
7791)
92+
93+ export function NuqsAdapter ( {
94+ children,
95+ stringifySearchWith = defaultStringifySearch ,
96+ ...adapterProps
97+ } : AdapterProps & {
98+ children : ReactNode
99+ stringifySearchWith ?: ( search : Record < string , any > ) => string
100+ } ) : ReactElement {
101+ return createElement (
102+ NuqsTanstackRouterAdapterContext . Provider ,
103+ { value : { stringifySearchWith } } ,
104+ createElement ( NuqsTanstackRouterAdapter , { ...adapterProps , children } )
105+ )
106+ }
0 commit comments