useWatcher top-level property

_JoltWatcherHookCreatorImpl useWatcher
final

Creates a watcher hook that observes specific reactive sources.

Watchers provide more control than effects by explicitly defining what to watch and comparing old vs new values before executing the callback.

Parameters:

  • sourcesFn: Function that returns the values to watch
  • fn: Callback function executed when sources change
  • when: Optional condition function for custom trigger logic
  • immediately: Whether to execute the callback immediately (default is false)
  • onDebug: Optional debug callback for reactive system debugging

Returns: A Watcher with fine-grained change detection

Example:

setup(context, props) {
  final count = useSignal(0);
  final name = useSignal('Alice');

  useWatcher(
    () => [count.value, name.value],
    (newValues, oldValues) {
      print('Changed from $oldValues to $newValues');
    },
  );

  return () => Text('${name.value}: ${count.value}');
}

Implementation

final useWatcher = _JoltWatcherHookCreatorImpl();