usePrevious
Tracks the value a source held before its latest change — a lag-by-one view. Cross-platform — import from @sigx/use.
import { signal } from 'sigx';
import { usePrevious } from '@sigx/use';
const count = signal(0);
const prev = usePrevious(count); // prev.value === undefined until the first change
count.value = 5; // prev.value === 0
count.value = 12; // prev.value === 5
It accepts any MaybeSignal source — a signal, a computed, or a getter — and returns a ReadSignal. Until the source changes for the first time it holds initialValue (default undefined), so the return type is ReadSignal<T | undefined>.
Values are kept by reference — objects are not proxied, so identity is preserved across the lag.
Signature
function usePrevious<T>(source: MaybeSignal<T>, initialValue?: T): ReadSignal<T | undefined>;
Platform: everywhere. The internal watcher disposes with the owning scope.
See also
useCounter— pair with a counter to diff against the last value.watchDebounced— react to changes with a collapsed callback.- Conventions —
MaybeSignalinputs andReadSignaloutputs.
