Skip to content

type AtomWithHashOptions #16

@AjaxSolutions

Description

@AjaxSolutions

I'm learning AtomWithHashOptions. Does this type make sense? If so, please add it at the next opportunity.

export type AtomWithHashOptions<Value> = {
    /**
     * A function to serialize the value before storing it in the URL hash.
     * The serialized string will be decoded by the `deserialize` function.
     * Default: `JSON.stringify`
     */
    serialize?: (val: Value) => string;
    /**
     * A function to deserialize the value retrieved from the URL hash.
     * The deserialized value will be used as the initial value of the atom.
     * If the deserialized value is `NO_STORAGE_VALUE`, the atom will be initialized with the `initialValue`.
     * Default: `(str) => NO_STORAGE_VALUE`
     */
    deserialize?: (str: string | null) => Value | typeof NO_STORAGE_VALUE;
    /**
     * A boolean flag that indicates whether to use `window.history.replaceState` instead of `window.location.hash`.
     * When this flag is set to `true`, the URL hash will be replaced instead of being added to the browser history.
     * Note: This flag is deprecated in favor of using `setHash` with `"replaceState"`.
     * Default: `false`
     * @deprecated Use `setHash` with 'replaceState' instead
     */
    replaceState?: boolean;
    /**
     * A function that will be called when the URL hash changes.
     * The callback should update the atom value with the new hash value.
     * Default: `(callback) => { window.addEventListener("hashchange", callback); return () => window.removeEventListener("hashchange", callback); }`
     * @returns A function that can be called to unsubscribe from the changes.
     */
    subscribe?: (callback: () => void) => () => void;
    /**
     * A function to set the URL hash.
     * By default, it sets the hash using `window.location.hash`.
     * When `replaceState` is set to true, it uses `window.history.replaceState` instead.
     * If a function is provided, it will be used instead.
     * The searchParams argument is the stringified version of the atom value.
     * Default: `"default"`
     * Options:
     * - `"default"`: Sets the hash using `window.location.hash`.
     * - `"replaceState"`: Sets the hash using `window.history.replaceState`.
     * - `(searchParams: string) => void`: A custom function that will be used to set the hash.
     */
    setHash?: "default" | "replaceState" | ((searchParams: string) => void);
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions