To encode any JSON serialiazable value to a string.
object
- The state object to encode.
An encoded string
import { encode } from 'state-in-url/encoder';
// to params
const params = new URLSearchParams();
Object.entries(state).forEach(([key, value]) => {
params.set(key, encode(value));
});
const str = params.toString();
To parse previously encoded string back to an object
payload: string
- A string to decode.defaults?: object
- Shape object, this values will be used as defaults.
A decoded object
import { decode } from 'state-in-url/encoder';
// from params
const obj = Object.fromEntries(
[...params.entries()].map(([key, value]) => [
key,
// decode(value, optionalFallback),
decode(value),
]),
)