Table of Contents

Struct StateUpdater<T>

Namespace
Velvet
Assembly
Velvet.Docs.dll

The single state setter returned by UseState<T>(T). Accepts either a replacement value (setValue(next)) or a functional updater (setValue(prev => next)). The functional form always reads the latest committed value, so it is safe to invoke from a closure captured by an earlier render (no stale-closure pitfall).

public readonly struct StateUpdater<T>

Type Parameters

T

State type.

Remarks

A struct (no allocation) wrapping the two cached closures built once at slot creation. The wrapped closures are reference-stable across renders, so a captured StateUpdater<T> stays valid. An update equal to the current value does not request a re-render. Equality is Object.is-style: float and double by raw bit pattern, so NaN equals itself and +0 does not equal -0; string by ordinal content, so a rebuilt-but-equal string bails; every other value type through EqualityComparer<T>.Default — a record struct included, where that is the synthesized field-wise equality, so a fresh one of equal content bails; and every other reference type by instance, so a fresh record class instance of identical content re-renders.

Methods

Equals(object?)

Indicates whether this instance and a specified object are equal.

Equals(StateUpdater<T>)

Value equality over the two wrapped (reference-stable) delegates. Two setters for the same state slot are equal across renders, so a StateUpdater<T> placed in a dependency array stays stable (UseCallback / UseEffect deps do not spuriously change).

GetHashCode()

Returns the hash code for this instance.

Invoke(Func<T, T>)

Computes the next state from the latest committed value (setValue(prev => next)). Reads the current value at invocation time, so it is safe to call from a stale closure.

Invoke(T)

Replaces the state with next.

Operators

implicit operator Action<Func<T, T>>(StateUpdater<T>)

Implicit conversion to the functional-updater System.Action<T> of Func<T, T>, for callers that want to pass the prev => next form as a delegate.

implicit operator Action<T>(StateUpdater<T>)

Implicit conversion to the direct value-setter System.Action<T> so the setter can be stored in / passed as an Action<T> (e.g. a callback parameter) and invoked with value-call syntax. The returned delegate is the cached, reference-stable value-setter.