Skip to content

[📖] useConstant is not documented #8237

@maiieul

Description

@maiieul

Suggestion

V2 code:

/**
 * Stores a value which is retained for the lifetime of the component. Subsequent calls to
 * `useConstant` will always return the first value given.
 *
 * If the value is a function, the function is invoked once to calculate the actual value.
 *
 * @public
 */
export const useConstant = <T>(value: (() => T) | T): T => {
  const { val, set } = useSequentialScope<T>();
  if (val != null) {
    return val;
  }
  // Note: We are not using `invoke` here because we don't want to clear the context
  value = isFunction(value) && !isQwikComponent(value) ? value() : value;
  return set(value as T);
};

Note: in V1 it's marked as deprecated but not in V2.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions