| 192 | * @param options - Parser (defines the state data type), optional default value and history mode. |
| 193 | */ |
| 194 | export 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 | } |