API reference / @evolu/common / Ref / Ref

Interface: Ref<T>

Defined in: packages/common/src/Ref.ts:32

Ref provides a simple API to hold and update a value, similar to a "ref" in functional programming or React. It exposes methods to get, set, and modify the current state.

Use a Ref instead of a variable when you want to pass state around as an object or update it in a controlled way. If you need subscriptions, see Store.

Updating in a controlled way means all changes go through specific methods (set or modify), making state updates predictable and easy to track.

Example

const count = createRef(0);
count.set(1);
count.modify((n) => n + 1);
console.log(count.get()); // 2

Example of using Ref as a dependency

interface CounterRefDep {
  readonly counterRef: Ref<number>;
}

Extended by

Type Parameters

Type Parameter
T

Properties

PropertyModifierTypeDescriptionDefined in
getreadonly() => TReturns the current state.packages/common/src/Ref.ts:34
modifyreadonly(updater) => voidModifies the state using an updater function.packages/common/src/Ref.ts:40
setreadonly(state) => voidSets the state.packages/common/src/Ref.ts:37

Was this page helpful?