• A custom hook that creates a state reference. The reference provides access to the state, along with event dispatching, validation, and confirmation logic.

    Type Parameters

    • T

    Parameters

    • initialValue: T | () => T

      The initial state value.

    Returns {
        onChange: ISimpleEvent<T>;
        set: (value: T) => void;
        confirm(): void;
        get(): T;
        useConfirm(on: (value: T) => T): void;
        useMemo<TOut>(on: (value: T) => TOut, deps?: any[]): TOut;
        useOnChange(on: (value: T) => void | Promise<void> | () => void): void;
        useValidate(on: (next: T, current: T) => T): void;
    }

    An object implementing StateRef along with additional helper hooks.

    • onChange: ISimpleEvent<T>
    • set: (value: T) => void
    • confirm:function
    • get:function
    • useConfirm:function
      • Sets a confirmation function to process the state value during confirmation.

        Parameters

        • on: (value: T) => T

          A function that confirms (and optionally transforms) the current state value.

        Returns void

    • useMemo:function
      • Memoizes a value based on the current state and additional dependencies.

        Type Parameters

        • TOut

        Parameters

        • on: (value: T) => TOut

          A function that computes a value based on the current state.

        • Optionaldeps: any[]

          Optional additional dependencies.

        Returns TOut

        The memoized value.

    • useOnChange:function
      • Registers a callback to be invoked when the state changes. Accepts a sync function, a cleanup function, or a function returning a Promise (which will be ignored).

        Parameters

        • on: (value: T) => void | Promise<void> | () => void

          The callback function that receives the new state value.

        Returns void

    • useValidate:function
      • Sets a validation function to process any new state value before updating.

        Parameters

        • on: (next: T, current: T) => T

          A function that validates (and optionally transforms) the new state value.

        Returns void