useWatcher top-level property

JoltWatcherHookCreator useWatcher
final

Creates a watcher hook that observes changes with fine-grained control.

A watcher provides more control than effects, allowing you to compare old and new values and define custom trigger conditions.

Parameters:

  • sources: Function that returns the values to watch
  • fn: Callback function executed when sources change
  • keys: Optional keys for hook memoization
  • debug: Optional debug options
  • immediately: Whether to execute the callback immediately (default is false)
  • when: Optional condition function for custom trigger logic

Returns: A Watcher with fine-grained change detection

Example:

Widget build(BuildContext context) {
  final count = useSignal(0);

  useWatcher(
    () => count.value,
    (newValue, oldValue) {
      print('Count changed from $oldValue to $newValue');
    },
    when: (new, old) => new > old, // Only when increasing
  );

  return ElevatedButton(
    onPressed: () => count.value++,
    child: Text('Count: ${count.value}'),
  );
}

Implementation

final useWatcher = JoltWatcherHookCreator._();