Table of Contents

Method UseState

Namespace
Velvet
Assembly
Velvet.Docs.dll

UseState<T>(T)

Local state hook. Must be used inside Render() only. Returns the 2-tuple shape (value, setValue) where setValue is a single StateUpdater<T> that accepts either a replacement value or a functional updater.

public static (T value, StateUpdater<T> setValue) UseState<T>(T initial)

Parameters

initial T

Initial value used on the first render. Ignored on subsequent renders.

Returns

(T value, StateUpdater<T> setValue)

2-tuple:

  • value: current value
  • setValue: StateUpdater<T> — call setValue.Invoke(next) to replace the value, or setValue.Invoke(prev => next) for the functional updater (reads the latest committed value, safe to invoke from a closure captured by an earlier render).

Type Parameters

T

State type.

UseState<T>(Func<T>)

Lazy-initialized variant taking a factory invoked once for the initial value. initialFactory is invoked exactly once on the first render to produce the initial state; subsequent renders skip the call. Use this when constructing the initial value is expensive (large Map/Set/List allocation, etc.) so the cost is not paid on every render.

public static (T value, StateUpdater<T> setValue) UseState<T>(Func<T> initialFactory)

Parameters

initialFactory Func<T>

Factory invoked on first render only. Must not be null.

Returns

(T value, StateUpdater<T> setValue)

2-tuple:

  • value: current value
  • setValue: StateUpdater<T> accepting a value or a functional updater.

Type Parameters

T

State type.