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
| Property | Modifier | Type | Description | Defined in |
|---|---|---|---|---|
get | readonly | () => T | Returns the current state. | packages/common/src/Ref.ts:34 |
modify | readonly | (updater) => void | Modifies the state using an updater function. | packages/common/src/Ref.ts:40 |
set | readonly | (state) => void | Sets the state. | packages/common/src/Ref.ts:37 |