fn
refWithControl
v0.0.15testeddemoA ref with fine-grained control over its reactivity: read/write without tracking or triggering, plus `onBeforeChange` (vetoable) and `onChanged` hooks. Built on `customRef`, so there are no extra watchers.
Examples
ts
const num = refWithControl(0);
num.value++; // tracked + triggered, like a normal ref
num.peek(); // read without tracking
num.lay(5); // write without triggering effectsts
// veto changes with onBeforeChange
const positive = refWithControl(1, {
onBeforeChange: (value) => value > 0, // reject non-positive values
onChanged: (value, old) => log(`${old} -> ${value}`),
});
positive.value = -1; // rejected, stays 1Demo
Loading demo…
Signature
ts
export function refWithControl<T>(
initial: T,
options: RefWithControlOptions<T> ={ ... }Type Parameters
TParameters
| Parameter | Type | Description |
|---|---|---|
initial | T | The initial value |
options? | RefWithControlOptions<T> | `onBeforeChange` (return `false` to veto) and `onChanged` hooks |
Returns
RefWithControlReturn<T>A ref extended with `get`/`set`/`peek`/`lay`/`untrackedGet`/`silentSet`