state-in-url
Version:
Store state in URL as in object, types and structure are preserved, with TS validation. Same API as React.useState, wthout any hasssle or boilerplate. Next.js@14-15 and react-router@6-7.
46 lines (45 loc) • 1.63 kB
TypeScript
import { type JSONCompatible } from "../utils";
/**
* A hook that returns stringify and parse functions for encoding and decoding state
* to and from URL search parameters.
*
* @template T - The type of the state shape
* @param {JSONCompatible<T>} [stateShape] Fallback values for state
* @returns {} `stringify` and `parse` functions
*
* * Example:
* ```ts
* export const form = { name: '' };
* const { parse, stringify } = useUrlEncode(form);
*
* stringify({ name: 'John' }, 'someExistingParamToKeep=123');
* // by default it's uses router.push with scroll: false
* parse('name=Tom');
* ```
*
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/useUrlEncode}
*/
export declare function useUrlEncode<T extends JSONCompatible>(stateShape: T): {
/**
Parse URL query string to state object
* * Example:
* ```ts
const state = parse("name='Tom'");
console.log(state); // Output: { name: 'Tom' }
* ```
*
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/useUrlEncode#parse}
*/
parse: (strOrSearchParams: string | URLSearchParams) => typeof stateShape;
/**
Converts a state object to a URL query string.
* * Example:
* ```ts
const queryString = stringify({ name: 'John' }, 'someExistingParamToKeep=123');
console.log(queryString); // Output: name='John'&someExistingParamToKeep=123
* ```
*
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/useUrlEncode#stringify}
*/
stringify: (state: typeof stateShape, paramsToKeep?: string | URLSearchParams) => string;
};