@elemental/common / Function

signalChange

Generic types:T

Used to run an effect when a signal reference changes. For example can be useful for running an effect when an input signal changes to replace the ngOnChanges lifecycle hook. The effect function will be called with the current value of the signal and the previous value.

Presentation

function signalChange(
  signalRef: Signal<T> | InputSignal<T> | InputSignalWithTransform<T, unknown>,
  effectFn: (current: T, previous: T | undefined) => void,
  options?: { onCleanupFn?: (() => void) | undefined } | undefined,
): EffectRef;

Returns

Parameters

NameTypeDescription
signalRef
Signal<T> | InputSignal<T> | InputSignalWithTransform<T, unknown>

@param signalRef The signal to be depend on

effectFn
(current: T, previous: T | undefined) => void

@param effectFn The function to be called when the signal changes

options
{ onCleanupFn?: (() => void) | undefined; } | undefined

@param options Options for the effect

  • onCleanupFn: A function to be called when the before on next effect run (not on the first run) and when the effect is destroyed

Example usage

myInputSignal = input('initial value');
signalChange(myInputSignal, (current, previous) => { console.log('myInputSignal changed', current, previous); });