This module provides functions for encoding and decoding state objects to and from URL query strings.
Encodes a state object into a URL query string.
state: object
- The state object to encode.defaults?: object
- Optional default values for the state object.paramsToKeep?: string | URLSearchParams
- Optional existing parameters to keep in the resulting query string.
A string representing the encoded URL query string.
import { encodeState } from 'state-in-url/encodeState';
const form = { name: '' };
const encodedState = encodeState({ name: 'test' }, form, 'someExistingParam=123');
console.log(encodedState); // Output: name=test&someExistingParam=123
Decodes a URI string into an object.
uriString: string | URLSearchParams
- The URI string or URLSearchParams object to decode.defaults?: T
- Optional default values for the resulting object.
The decoded object.
import { decodeState } from 'state-in-url/encodeState';
const form = { name: '', key: '' };
const decodedState = decodeState('key=value&name=Alex', form);
console.log(decodedState); // Output: { name: 'Alex', key: 'value }