MCPcopy Create free account
hub / github.com/47ng/nuqs / useQueryState

Function useQueryState

packages/nuqs/src/useQueryState.ts:194–229  ·  view source on GitHub ↗
(
  key: string,
  options: Partial<UseQueryStateOptions<T>> & {
    defaultValue?: T
  } = {}
)

Source from the content-addressed store, hash-verified

192 * @param options - Parser (defines the state data type), optional default value and history mode.
193 */
194export function useQueryState<T = string>(
195 key: string,
196 options: Partial<UseQueryStateOptions<T>> & {
197 defaultValue?: T
198 } = {}
199) {
200 const { parse, type, serialize, eq, defaultValue, ...hookOptions } = options
201 const [{ [key]: state }, setState] = useQueryStates(
202 {
203 [key]: {
204 parse: parse ?? ((x: any) => x as unknown as T),
205 type,
206 serialize,
207 eq,
208 defaultValue
209 } as GenericParser<T>
210 },
211 hookOptions
212 )
213 const update = useCallback(
214 (stateUpdater: React.SetStateAction<T | null>, callOptions: Options = {}) =>
215 setState(
216 old => ({
217 [key]:
218 typeof stateUpdater === 'function'
219 ? // @ts-expect-error somehow stateUpdater is not narrowed correctly
220 // and useQueryStates' key type is not inferred
221 stateUpdater(old[key])
222 : stateUpdater
223 }),
224 callOptions
225 ),
226 [key, setState]
227 )
228 return [state, update]
229}

Callers 15

useTestHookWithDefaultsFunction · 0.90
useTestHookFunction · 0.90
TestComponentFunction · 0.90
DemoFunction · 0.90
StarsGraphFunction · 0.90
PrettyURLsDemoPageFunction · 0.90
CrosslinkDemoPageFunction · 0.90
ReproPageFunction · 0.90
BasicCounterDemoPageFunction · 0.90
BuilderPatternDemoPageFunction · 0.90

Calls 1

useQueryStatesFunction · 0.90

Tested by 3

useTestHookWithDefaultsFunction · 0.72
useTestHookFunction · 0.72
TestComponentFunction · 0.72