### Development: Run Example App Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md To run an example application, link the local package, install dependencies in the example directory, and start the app. ```bash npm link cd examples/react-router npm install npm link use-query-params npm start ``` -------------------------------- ### Run a Specific Example Source: https://github.com/pbeshai/use-query-params/blob/master/README.md Starts a specific example application within the monorepo. ```bash lerna run --scope react-router-example start ``` -------------------------------- ### Set Up Example Packages Source: https://github.com/pbeshai/use-query-params/blob/master/README.md Bootstraps and links example packages within the monorepo for local development. ```bash lerna bootstrap --scope "*-example" lerna link ``` -------------------------------- ### Install serialize-query-params Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Install the library using npm. This is the initial step before using its functionalities. ```bash npm install --save serialize-query-params ``` -------------------------------- ### Install use-query-params Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md Install the use-query-params package using npm. ```bash npm install --save use-query-params ``` -------------------------------- ### Start Create React App Source: https://github.com/pbeshai/use-query-params/blob/master/examples/react-router-5/README.md Runs the Create React App development server. Open http://localhost:3000 to view the app. ```bash npm start ``` -------------------------------- ### Install and Bootstrap Monorepo Dependencies Source: https://github.com/pbeshai/use-query-params/blob/master/README.md Installs project dependencies, bootstraps specific packages within the monorepo, builds the project, and runs tests. ```bash npm install npx lerna bootstrap --hoist --scope "use-query-params" --scope "serialize-query-params" npm build npm test ``` -------------------------------- ### Basic useQueryParams Example Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md Demonstrates reading multiple query parameters ('foo' and 'bar') using a configuration object and updating them. Shows setting specific parameters, multiple parameters, and functional updates. ```javascript import { useQueryParams, StringParam, NumberParam } from 'use-query-params'; // reads query parameters `foo` and `bar` from the URL and stores their decoded values const [query, setQuery] = useQueryParams({ foo: NumberParam, bar: StringParam }); setQuery({ foo: 500 }) setQuery({ foo: 123, bar: 'zzz' }, 'push'); // to unset or remove a parameter set it to undefined and use pushIn or replaceIn update types setQuery({ foo: undefined }) // ?foo=123&bar=zzz becomes ?bar=zzz // functional updates are also supported: setQuery((latestQuery) => ({ foo: latestQuery.foo + 150 })) ``` -------------------------------- ### Basic useQueryParam Example Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md Demonstrates reading a numeric query parameter 'foo' and updating it. Shows how to set a value and how to unset it by passing undefined. Supports functional updates. ```javascript import { useQueryParam, NumberParam } from 'use-query-params'; // reads query parameter `foo` from the URL and stores its decoded numeric value const [foo, setFoo] = useQueryParam('foo', NumberParam); setFoo(500); setFoo(123, 'push'); // to unset or remove a parameter set it to undefined and use pushIn or replaceIn update types setFoo(undefined) // ?foo=123&bar=zzz becomes ?bar=zzz // functional updates are also supported: setFoo((latestFoo) => latestFoo + 150) ``` -------------------------------- ### React Router 6 Adapter Setup Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md Set up the QueryParamProvider with the React Router 6 adapter for integrating with React Router 6. ```javascript import React from 'react'; import ReactDOM from 'react-dom/client'; import { QueryParamProvider } from 'use-query-params'; import { ReactRouter6Adapter } from 'use-query-params/adapters/react-router-6'; import { BrowserRouter, Route, Routes } from 'react-router-dom'; import App from './App'; const root = ReactDOM.createRoot( document.getElementById('root') as HTMLElement ); root.render( }> , document.getElementById('root') ); ``` -------------------------------- ### withQueryParams Example Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md Demonstrates using the withQueryParams HOC to inject 'query' and 'setQuery' props into a component, allowing it to interact with URL query parameters. ```javascript import { withQueryParams, StringParam, NumberParam } from 'use-query-params'; const MyComponent = ({ query, setQuery, ...others }) => { const { foo, bar } = query; return
foo = {foo}, bar = {bar}
} // reads query parameters `foo` and `bar` from the URL and stores their decoded values export default withQueryParams({ foo: NumberParam, bar: StringParam }, MyComponent); ``` -------------------------------- ### Custom Comma Array Param Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Define a custom parameter type using existing serialization utility functions. This example creates a CommaArrayParam that uses a comma to delimit array entries. ```javascript import { encodeDelimitedArray, decodeDelimitedArray } from 'serialize-query-params'; /** Uses a comma to delimit entries. e.g. ['a', 'b'] => qp?=a,b */ const CommaArrayParam = { encode: (array: string[] | null | undefined): string | undefined => encodeDelimitedArray(array, ','), decode: (arrayStr: string | string[] | null | undefined): string[] | undefined => decodeDelimitedArray(arrayStr, ',') }; ``` -------------------------------- ### Custom Comma Delimited Array Parameter Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md Defines a custom parameter type for arrays that uses a comma as a delimiter. This example shows how to create custom encoding and decoding logic using provided utility functions. ```javascript import { encodeDelimitedArray, decodeDelimitedArray } from 'use-query-params'; /** Uses a comma to delimit entries. e.g. ['a', 'b'] => qp?=a,b */ const CommaArrayParam = { encode: (array: string[] | null | undefined) => encodeDelimitedArray(array, ','), decode: (arrayStr: string | string[] | null | undefined) => decodeDelimitedArray(arrayStr, ',') }; ``` -------------------------------- ### Encode Query Parameters Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md The encodeQueryParams function converts query parameter values to strings using provided encoding functions. This is useful for constructing URLs. Ensure 'query-string' is installed if you need its stringify function. ```javascript import { encodeQueryParams, NumberParam } from 'use-query-params'; // since v1.0 stringify is not exported from 'use-query-params', // so you must install the 'query-string' package in case you need it import { stringify } from 'query-string'; // encode each parameter according to the configuration const encodedQuery = encodeQueryParams({ foo: NumberParam }, { foo }); const link = `/?${stringify(encodedQuery)}`; ``` -------------------------------- ### Build use-query-params Source: https://github.com/pbeshai/use-query-params/blob/master/examples/react-router-5/README.md Build the use-query-params package from the root directory. ```bash yarn build ``` -------------------------------- ### Build Create React App for Production Source: https://github.com/pbeshai/use-query-params/blob/master/examples/react-router-5/README.md Builds the Create React App for production, optimizing for performance and creating minified files. ```bash npm run build ``` -------------------------------- ### Add packages to project Source: https://github.com/pbeshai/use-query-params/blob/master/examples/react-router-5/README.md Add use-query-params and serialize-query-params to the current project using yalc. ```bash yalc add use-query-params yalc add serialize-query-params ``` -------------------------------- ### Test Create React App Source: https://github.com/pbeshai/use-query-params/blob/master/examples/react-router-5/README.md Launches the test runner in the interactive watch mode for Create React App. ```bash npm test ``` -------------------------------- ### Basic useQueryParam and useQueryParams Usage Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md Demonstrates the typical usage of the useQueryParam and useQueryParams hooks for managing array parameters. ```javascript import { ArrayParam, useQueryParam, useQueryParams } from 'use-query-params'; // typically used with the hooks: const [foo, setFoo] = useQueryParam('foo', ArrayParam); // - OR - const [query, setQuery] = useQueryParams({ foo: ArrayParam }); ``` -------------------------------- ### Publish to yalc Source: https://github.com/pbeshai/use-query-params/blob/master/examples/react-router-5/README.md Publish the use-query-params package to yalc from its directory, skipping scripts. ```bash yalc publish --no-scripts ``` -------------------------------- ### useQueryParams with Custom Parameter Type Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md Illustrates how to define and use a custom parameter type with { encode, decode } functions for use with useQueryParams. Shows encoding and decoding logic. ```javascript import { useQueryParams } from 'use-query-params'; const MyParam = { encode(value) { return `${value * 10000}`; }, decode(strValue) { return parseFloat(strValue) / 10000; } } // ?foo=10000 -> query = { foo: 1 } const [query, setQuery] = useQueryParams({ foo: MyParam }); // goes to ?foo=99000 setQuery({ foo: 99 }) ``` -------------------------------- ### Using useQueryParams Hook Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md Use the `useQueryParams` hook to manage multiple query parameters at once. It returns an object with current values and a setter function. ```javascript import * as React from 'react'; import { useQueryParams, StringParam, NumberParam, ArrayParam, withDefault, } from 'use-query-params'; // create a custom parameter with a default value const MyFiltersParam = withDefault(ArrayParam, []) const UseQueryParamsExample = () => { // something like: ?x=123&q=foo&filters=a&filters=b&filters=c in the URL const [query, setQuery] = useQueryParams({ x: NumberParam, q: StringParam, filters: MyFiltersParam, }); const { x: num, q: searchQuery, filters } = query; return (

num is {num}

searchQuery is {searchQuery}

There are {filters.length} filters active.

); }; export default UseQueryParamsExample; ``` -------------------------------- ### Using useQueryParam Hook Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md Use the `useQueryParam` hook to manage individual query parameters. It returns the current value and a setter function. ```javascript import * as React from 'react'; import { useQueryParam, NumberParam, StringParam } from 'use-query-params'; const UseQueryParamExample = () => { // something like: ?x=123&foo=bar in the URL const [num, setNum] = useQueryParam('x', NumberParam); const [foo, setFoo] = useQueryParam('foo', StringParam); return (

num is {num}

foo is {foo}

); }; export default UseQueryParamExample; ``` -------------------------------- ### useQueryParams Hook Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md Manages multiple query parameters simultaneously. It accepts a configuration map or an array of names, returning an object with decoded values and a setter for the entire query object. Supports functional updates and overriding provider options. ```APIDOC ## useQueryParams ### Description Manages multiple query parameters simultaneously. It accepts a configuration map or an array of names, returning an object with decoded values and a setter for the entire query object. Supports functional updates and overriding provider options. ### Signatures Option 1: Pass a config map. ```js useQueryParams( paramConfigMap: QPCMap, options?: QueryParamOptions ): [DecodedValueMap, SetQuery]; ``` Option 2: Pass an array of param names. ```js useQueryParams( names: string[], options?: QueryParamOptions ): [DecodedValueMap, SetQuery]; ``` Option 3: Pass no arguments to get all predefined params. ```js useQueryParams( ): [DecodedValueMap, SetQuery]; ``` ### Parameters #### Option 1 & 2 - **paramConfigMap** (QPCMap) or **names** (string[]) - Required - Configuration for query parameters. - **options** (QueryParamOptions) - Optional - Overrides for options from `QueryParamProvider`. #### Option 3 No parameters. ### Returns A tuple containing: - An object with the decoded values of the query parameters. - A setter function `(newQuery, updateType)` to update multiple query parameters. ### Example ```js import { useQueryParams, StringParam, NumberParam } from 'use-query-params'; const [query, setQuery] = useQueryParams({ foo: NumberParam, bar: StringParam }); setQuery({ foo: 500 }); setQuery({ foo: undefined }); // To unset a parameter setQuery((latestQuery) => ({ foo: latestQuery.foo + 1 })); ``` ### Example with Custom Parameter Type ```js import { useQueryParams } from 'use-query-params'; const MyParam = { encode(value) { return `${value * 10000}`; }, decode(strValue) { return parseFloat(strValue) / 10000; } } const [query, setQuery] = useQueryParams({ foo: MyParam }); setQuery({ foo: 99 }); // Goes to ?foo=99000 ``` ``` -------------------------------- ### QueryParamProvider with Custom Options Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md You can provide custom options to the QueryParamProvider, such as searchStringToObject and objectToSearchString functions from the 'query-string' package. ```jsx // optionally specify options import { parse, stringify } from 'query-string'; const options = { searchStringToObject: parse, objectToSearchString: stringify, } ``` -------------------------------- ### Eject Create React App Source: https://github.com/pbeshai/use-query-params/blob/master/examples/react-router-5/README.md Removes the single build dependency from a Create React App project, giving full control over configuration files. ```bash npm run eject ``` -------------------------------- ### Run TypeScript Compiler in Watch Mode Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Command to run the TypeScript compiler in watch mode for development. ```bash npm run dev ``` -------------------------------- ### Convert Object to Search String Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Converts an object (e.g., { foo: '123', bar: 'x' }) into a URL search string. Does not support null values and does not prefix with '?'. This is a basic implementation. ```javascript import { objectToSearchString } from 'serialize-query-params'; const obj = objectToSearchString({ foo: ['a', 'z'], bar: 'x' }); // '?foo=a&foo=z&bar=x' ``` -------------------------------- ### Set Default Value for Array Param Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Wrap an existing param with withDefault() to set a default value. By default, nulls are converted to defaults. Pass false as a third argument to prevent nulls from being included. ```javascript import { withDefault, ArrayParam } from 'serialize-query-params'; // by default, nulls are converted to defaults const NeverNullArrayParam = withDefault(ArrayParam, []); // if you don't want nulls to be included, pass false as a third arg const NeverUndefinedArrayParam = withDefault(ArrayParam, [], false); ``` -------------------------------- ### useQueryParams Hook Signatures Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md The signatures for the useQueryParams hook, which can read multiple query parameters based on a configuration map, an array of names, or all predefined parameters. ```typescript // option 1: pass only a config with possibly some options useQueryParams( paramConfigMap: QPCMap, options?: QueryParamOptions ): [DecodedValueMap, SetQuery]; // option 2: pass an array of param names, relying on predefined params for types useQueryParams( names: string[], options?: QueryParamOptions ): [DecodedValueMap, SetQuery]; // option 3: pass no args, get all params back that were predefined in a proivder useQueryParams( ): [DecodedValueMap, SetQuery]; ``` -------------------------------- ### Using QueryParams Render Props Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md The `` component provides query parameters and a setter function via render props, allowing access within its children. ```javascript import * as React from 'react'; import { QueryParams, StringParam, NumberParam, ArrayParam, withDefault, } from 'use-query-params'; // create a custom parameter with a default value const MyFiltersParam = withDefault(ArrayParam, []) const RenderPropsExample = () => { const queryConfig = { x: NumberParam, q: StringParam, filters: MyFiltersParam, }; return (
{({ query, setQuery }) => { const { x: num, q: searchQuery, filters } = query; return ( <>

num is {num}

searchQuery is {searchQuery}

There are {filters.length} filters active.

); }}
); }; export default RenderPropsExample; ``` -------------------------------- ### Custom Query String Parsing with query-string Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md Configure QueryParamProvider to use the query-string library for advanced parsing and stringifying of URL query parameters. ```javascript import React from 'react'; import ReactDOM from 'react-dom/client'; import { QueryParamProvider } from 'use-query-params'; import { ReactRouter6Adapter } from 'use-query-params/adapters/react-router-6'; import { BrowserRouter, Route, Routes } from 'react-router-dom'; // optionally use the query-string parse / stringify functions to // handle more advanced cases than URLSearchParams supports. import { parse, stringify } from 'query-string'; import App from './App'; const root = ReactDOM.createRoot( document.getElementById('root') as HTMLElement ); root.render( }> , document.getElementById('root') ); ``` -------------------------------- ### useQueryParam Hook Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md Reads a single query parameter from the URL, provides its decoded value, and a setter function to update it. It can inherit configurations from a provider or use a default. The setter supports various update types and functional updates. ```APIDOC ## useQueryParam ### Description Reads a single query parameter from the URL, provides its decoded value, and a setter function to update it. It can inherit configurations from a provider or use a default. The setter supports various update types and functional updates. ### Signature ```js useQueryParam( name: string, paramConfig?: QueryParamConfig, options?: QueryParamOptions ): [TypeFromDecode, (newValue: NewValueType, updateType?: UrlUpdateType) => void] ``` ### Parameters #### Path Parameters - **name** (string) - Required - The name of the query parameter to read. - **paramConfig** (QueryParamConfig) - Optional - An object with `encode` and `decode` functions for the query parameter. Falls back to provider config or `StringParam`. - **options** (QueryParamOptions) - Optional - Overrides for options from `QueryParamProvider`. ### Returns A tuple containing: - The decoded value of the query parameter. - A setter function `(newValue, updateType)` to update the query parameter. ### Example ```js import { useQueryParam, NumberParam } from 'use-query-params'; const [foo, setFoo] = useQueryParam('foo', NumberParam); setFoo(500); setFoo(undefined); // To unset the parameter setFoo((latestFoo) => latestFoo + 1); ``` ``` -------------------------------- ### Using withQueryParams HOC Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md The `withQueryParams` higher-order component injects query parameters and a setter function as props into a wrapped component. ```javascript import * as React from 'react'; import { withQueryParams, StringParam, NumberParam, ArrayParam, withDefault, } from 'use-query-params'; const WithQueryParamsExample = ({ query, setQuery }: any) => { const { x: num, q: searchQuery, filters } = query; return (

num is {num}

searchQuery is {searchQuery}

There are {filters.length} filters active.

); }; // create a custom parameter with a default value const MyFiltersParam = withDefault(ArrayParam, []) export default withQueryParams({ x: NumberParam, q: StringParam, filters: MyFiltersParam, }, WithQueryParamsExample); ``` -------------------------------- ### Param Types Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Defines various built-in parameter types for encoding and decoding URL query parameters. Custom parameter types can also be created. ```APIDOC ## Param Types See [all param definitions here](./src/params.ts). You can define your own parameter types by creating an object with an `encode` and a `decode` function. See the existing definitions for examples. Note that all null and empty values are typically treated as follows: | value | encoding | | --- | --- | | `null` | `?foo` | | `""` | `?foo=` | | `undefined` | `?` (removed from URL) | Examples in this table assume query parameter named `qp`. | Param | Type | Example Decoded | Example Encoded | | --- | --- | --- | --- | | StringParam | string | `'foo'` | `?qp=foo` | | NumberParam | number | `123` | `?qp=123` | | ObjectParam | { key: string } | `{ foo: 'bar', baz: 'zzz' }` | `?qp=foo-bar_baz-zzz` | | ArrayParam | string[] | `['a','b','c']` | `?qp=a&qp=b&qp=c` | | JsonParam | any | `{ foo: 'bar' }` | `?qp=%7B%22foo%22%3A%22bar%22%7D` | | DateParam | Date | `Date(2019, 2, 1)` | `?qp=2019-03-01` | | DateTimeParam | Date | `Date(2019, 2, 1)` | `?qp=2019-02-28T22:00:00.000Z` | | BooleanParam | boolean | `true` | `?qp=1` | | NumericObjectParam | { key: number } | `{ foo: 1, bar: 2 }` | `?qp=foo-1_bar-2` | | DelimitedArrayParam | string[] | `['a','b','c']` | `?qp=a_b_c'` | | DelimitedNumericArrayParam | number[] | `[1, 2, 3]` | `?qp=1_2_3'` | **Enum Param** You can define enum param using `createEnumParam`. It works as `StringParam` but restricts decoded output to a list of allowed strings: ```js import { createEnumParam } from 'serialize-query-params'; // values other than 'asc' or 'desc' will be decoded as undefined const SortOrderEnumParam = createEnumParam(['asc', 'desc']) ``` **Array Enum Param** You can define array enum param using `createEnumArrayParam` or `createEnumDelimitedArrayParam`. It will restricts decoded output to a list of allowed values. ```js import { createEnumArrayParam } from 'serialize-query-params'; // feel free to use Enum instead of union types type Color = 'red' | 'green' | 'blue' // values other than 'red', 'green' or 'blue' will be decoded as undefined const ColorArrayEnumParam = createEnumArrayParam(['red', 'green', 'blue']) ``` **Setting a default value** If you'd like to have a default value, you can wrap your param with `withDefault()`: ```js import { withDefault, ArrayParam } from 'serialize-query-params'; // by default, nulls are converted to defaults const NeverNullArrayParam = withDefault(ArrayParam, []); // if you don't want nulls to be included, pass false as a third arg const NeverUndefinedArrayParam = withDefault(ArrayParam, [], false); ``` **Example with Custom Param** You can define your own params if the ones shipped with this package don't work for you. There are included [serialization utility functions](./src/serialize.ts) to make this easier, but you can use whatever you like. ```js import { encodeDelimitedArray, decodeDelimitedArray } from 'serialize-query-params'; /** Uses a comma to delimit entries. e.g. ['a', 'b'] => qp?=a,b */ const CommaArrayParam = { encode: (array: string[] | null | undefined): string | undefined => encodeDelimitedArray(array, ','), decode: (arrayStr: string | string[] | null | undefined): string[] | undefined => decodeDelimitedArray(arrayStr, ',') }; ``` ``` -------------------------------- ### updateLocation Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Creates a new location-like object with an updated query string. ```APIDOC ## updateLocation ### Description Creates a new location-like object with the new query string (the `search` field) based on the encoded query parameters passed in via `encodedQuery`. Parameters not specified in `encodedQuery` will be dropped from the URL. ### Signature ```typescript function updateLocation( encodedQuery: EncodedQuery, location: Location, objectToSearchStringFn = objectToSearchString ): Location { ``` ### Example ```js import { updateLocation } from 'serialize-query-params'; // location has search: ?foo=123&bar=abc const newLocation = updateLocation({ foo: '555' }, location); // newLocation has search: ?foo=555 // note that unspecified query parameters (bar in this case) are not retained. ``` ``` -------------------------------- ### QueryParamProvider with Nested Parameters Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md Parameters can be nested within QueryParamProvider components to be automatically available to useQueryParams calls within their scope. ```jsx // optionally nest parameters in options to get automatically on useQueryParams calls {/* useQueryParams calls have access to foo here */} ... ... {/* useQueryParams calls have access to foo and bar here */} ... ``` -------------------------------- ### QueryParamProvider with React Router 6 Adapter Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md The QueryParamProvider component connects your router's history to the useQueryParams hook. Choose an adapter based on your router version (e.g., ReactRouter6Adapter). ```jsx // choose an adapter, depending on your router import { ReactRouter6Adapter } from 'use-query-params/adapters/react-router-6'; import { ReactRouter5Adapter } from 'use-query-params/adapters/react-router-5'; ``` -------------------------------- ### updateInLocation Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Creates a new location-like object with updated query parameters, retaining other parameters. ```APIDOC ## updateInLocation ### Description Creates a new location-like object with the new query string (the `search` field) based on the encoded query parameters passed in via `encodedQueryReplacements`. Only parameters specified in `encodedQueryReplacements` are affected by this update, all other parameters are retained. ### Signature ```typescript function updateInLocation( encodedQueryReplacements: EncodedQuery, location: Location, objectToSearchStringFn = objectToSearchString, searchStringToObjectFn = searchStringToObject ): Location { ``` ### Example ```js import { updateInLocation } from 'serialize-query-params'; // location has search: ?foo=123&bar=abc const newLocation = updateLocation({ foo: '555' }, location); // newLocation has search: ?foo=555&bar=abc // note that unspecified query parameters (bar in this case) are retained. ``` ``` -------------------------------- ### Convert Search String to Object Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Converts a URL search string (e.g., '?foo=123&bar=x') into an object. Handles duplicate keys by creating arrays. This is a basic implementation. ```javascript import { searchStringToObject } from 'serialize-query-params'; const obj = searchStringToObject('?foo=a&bar=x&foo=z'); // -> { foo: ['a', 'z'], bar: 'x'} ``` -------------------------------- ### Create Enum Param Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Define an enum parameter that restricts decoded output to a list of allowed strings. Values not in the list will be decoded as undefined. ```javascript import { createEnumParam } from 'serialize-query-params'; // values other than 'asc' or 'desc' will be decoded as undefined const SortOrderEnumParam = createEnumParam(['asc', 'desc']) ``` -------------------------------- ### QueryParams Component for Render Props Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md Use the QueryParams component to provide query and setQuery props to its children. It requires a config object mapping query parameter names to their encoding/decoding functions. ```jsx {({ query, setQuery }) =>
foo = {query.foo}
}
``` -------------------------------- ### Update in Location with Encoded Query Replacements Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Creates a new location object with an updated query string, replacing only specified parameters while retaining others. Uses provided functions for string conversion. ```javascript import { updateInLocation } from 'serialize-query-params'; // location has search: ?foo=123&bar=abc const newLocation = updateLocation({ foo: '555' }, location); // newLocation has search: ?foo=555&bar=abc // note that unspecified query parameters (bar in this case) are retained. ``` -------------------------------- ### Encode Query Parameters Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Converts query parameter values to strings using configured encode functions. Useful for constructing links with decoded query parameters. ```javascript import { encodeQueryParams, DelimitedArrayParam, NumberParam, } from 'serialize-query-params'; import { stringify } from 'query-string'; // encode each parameter according to the configuration const encodedQuery = encodeQueryParams( { foo: NumberParam, bar: DelimitedArrayParam }, { foo: 123, bar: ['a', 'b'] } ); // produces: { foo: '123', bar: 'a_b' } const link = `/?${stringify(encodedQuery)}`; ``` -------------------------------- ### withQueryParams Higher-Order Component Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md A higher-order component that injects query parameter props (`query` and `setQuery`) into a React component based on a provided configuration map. This allows components to access and modify URL query parameters without directly using hooks. ```APIDOC ## withQueryParams ### Description A higher-order component that injects query parameter props (`query` and `setQuery`) into a React component based on a provided configuration map. This allows components to access and modify URL query parameters without directly using hooks. ### Signature ```js withQueryParams>( paramConfigMap: QPCMap, WrappedComponent: React.ComponentType

): React.FC>> ``` ### Parameters - **paramConfigMap** (QPCMap) - Required - Configuration for query parameters. - **WrappedComponent** (React.ComponentType) - Required - The component to inject props into. ### Returns A React functional component that injects `query` and `setQuery` props. ### Example ```js import { withQueryParams, StringParam, NumberParam } from 'use-query-params'; const MyComponent = ({ query, setQuery, ...others }) => { const { foo, bar } = query; return

foo = {foo}, bar = {bar}
} export default withQueryParams({ foo: NumberParam, bar: StringParam }, MyComponent); ``` ``` -------------------------------- ### useQueryParam Hook Signature Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md The signature for the useQueryParam hook, which reads a single query parameter and returns its decoded value and a setter function. ```typescript useQueryParam( name: string, paramConfig?: QueryParamConfig, options?: QueryParamOptions ): [ TypeFromDecode, (newValue: NewValueType, updateType?: UrlUpdateType) => void ] ``` -------------------------------- ### Create Enum Array Param Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Define an array enum parameter that restricts decoded output to a list of allowed values. Use createEnumArrayParam or createEnumDelimitedArrayParam. ```javascript import { createEnumArrayParam } from 'serialize-query-params'; // feel free to use Enum instead of union types type Color = 'red' | 'green' | 'blue' // values other than 'red', 'green' or 'blue' will be decoded as undefined const ColorArrayEnumParam = createEnumArrayParam(['red', 'green', 'blue']) ``` -------------------------------- ### Update Location with Encoded Query Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Creates a new location object with an updated query string based on provided encoded parameters. Unspecified parameters are dropped. ```javascript import { updateLocation } from 'serialize-query-params'; // location has search: ?foo=123&bar=abc const newLocation = updateLocation({ foo: '555' }, location); // newLocation has search: ?foo=555 // note that unspecified query parameters (bar in this case) are not retained. ``` -------------------------------- ### encodeQueryParams Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Converts query parameter values to strings using configured encode functions, useful for constructing links. ```APIDOC ## encodeQueryParams ### Description Converts the values in query to strings via the encode functions configured in paramConfigMap. This can be useful for constructing links using decoded query parameters. ### Signature ```typescript encodeQueryParams( paramConfigMap: QPCMap, query: Partial> ): Partial> ``` ### Example ```js import { encodeQueryParams, DelimitedArrayParam, NumberParam, } from 'serialize-query-params'; import { stringify } from 'query-string'; // encode each parameter according to the configuration const encodedQuery = encodeQueryParams( { foo: NumberParam, bar: DelimitedArrayParam }, { foo: 123, bar: ['a', 'b'] } ); // produces: { foo: '123', bar: 'a_b' } const link = `/?${stringify(encodedQuery)}`; ``` ``` -------------------------------- ### searchStringToObject Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Converts a URL search string into an object of query parameters. ```APIDOC ## searchStringToObject ### Description Default implementation of searchStringToObject powered by URLSearchParams. This converts a search string like `?foo=123&bar=x` to `{ foo: '123', bar: 'x' }`. This is only a very basic version, you may prefer the advanced versions offered by third party libraries like query-string ("parse") or qs. ### Signature ```typescript function searchStringToObject(searchString: string): EncodedQuery ``` ### Example ```js import { searchStringToObject } from 'serialize-query-params'; const obj = searchStringToObject('?foo=a&bar=x&foo=z'); // -> { foo: ['a', 'z'], bar: 'x'} ``` ``` -------------------------------- ### objectToSearchString Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Converts an object of query parameters into a URL search string. ```APIDOC ## objectToSearchString ### Description Default implementation of objectToSearchString powered by URLSearchParams. Does not support null values. Does not prefix with "?" This converts an object `{ foo: '123', bar: 'x' }` to a search string `?foo=123&bar=x` This is only a very basic version, you may prefer the advanced versions offered by third party libraries like query-string ("stringify") or qs. ### Signature ```typescript function objectToSearchString(encodedParams: EncodedQuery): string ``` ### Example ```js import { objectToSearchString } from 'serialize-query-params'; const obj = objectToSearchString({ foo: ['a', 'z'], bar: 'x' }); // '?foo=a&foo=z&bar=x' ``` ``` -------------------------------- ### Decode Query Parameters Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Converts query parameter values from strings to their natural types using configured decode functions. Useful for parsing incoming URL query strings. ```javascript import { stringify, decodeQueryParams, NumberParam, DelimitedArrayParam } from 'serialize-query-params'; // encode each parameter according to the configuration const decodedQuery = decodeQueryParams( { foo: NumberParam, bar: DelimitedArrayParam }, { foo: '123', bar: 'a_b' } ); // produces: { foo: 123, bar: ['a', 'b'] } ``` -------------------------------- ### decodeQueryParams Source: https://github.com/pbeshai/use-query-params/blob/master/packages/serialize-query-params/README.md Converts query parameter values from strings to their natural types using configured decode functions. ```APIDOC ## decodeQueryParams ### Description Converts the values in query from strings to their natural types via the decode functions configured in paramConfigMap. ### Signature ```typescript decodeQueryParams(paramConfigMap: QPCMap, encodedQuery: Partial>): DecodedValueMap ``` ### Example ```js import { stringify, decodeQueryParams, NumberParam, DelimitedArrayParam } from 'serialize-query-params'; // encode each parameter according to the configuration const decodedQuery = decodeQueryParams( { foo: NumberParam, bar: DelimitedArrayParam }, { foo: '123', bar: 'a_b' } ); // produces: { foo: 123, bar: ['a', 'b'] } ``` ``` -------------------------------- ### withQueryParams HOC Signature Source: https://github.com/pbeshai/use-query-params/blob/master/packages/use-query-params/README.md The signature for the withQueryParams Higher-Order Component (HOC), which injects query parameter props into a wrapped React component. ```typescript withQueryParams> (paramConfigMap: QPCMap, WrappedComponent: React.ComponentType

): React.FC>> ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.