Skip to content

parseAsCodec

function parseAsCodec<T>(codec, options?): Parser<T>;

Defined in: tanstack/core/built-in-parsers.ts:514

Store a structured value as an opaque token in the URL via a codec. Decode failures fall back to null (the parser contract), so a tampered/stale token resolves to the default instead of throwing.

Equality defaults to comparing JSON.stringify of the decoded values (not the tokens, since an encoder may be non-deterministic); pass options.eq to override.

Type Parameter
T
Parameter Type
codec ValueCodec<T>
options? { eq?: (a, b) => boolean; }
options.eq? (a, b) => boolean

Parser<T>

With a cursor codec

import { createCursorCodec } from "@xtandard/lib/pagination";
import { parseAsCodec, useQueryState } from "@xtandard/lib/tanstack";
const cursor = parseAsCodec(createCursorCodec<{ id: number }>()); // JSON + base64url
cursor.serialize({ id: 42 }); // → "eyJpZCI6NDJ9"
cursor.parse("eyJpZCI6NDJ9"); // → { id: 42 }
cursor.parse("!!!"); // → null (tampered token)
const [after, setAfter] = useQueryState("after", cursor);