call method

Effect call(
  1. void effect(), {
  2. bool lazy = false,
  3. JoltDebugFn? onDebug,
})

Creates an effect hook that runs in response to reactive dependencies.

Effects run automatically when their reactive dependencies change. Use onEffectCleanup inside the effect to register cleanup functions.

Parameters:

  • effect: The effect function to execute
  • lazy: Whether to run the effect immediately upon creation. If true, the effect will execute once immediately when created, then automatically re-run whenever its reactive dependencies change. If false (default), the effect will only run when dependencies change, not immediately upon creation.
  • onDebug: Optional debug callback for reactive system debugging

Returns: An Effect that tracks dependencies and runs automatically

Example:

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

  useEffect(() {
    print('Count changed: ${count.value}');

    final timer = Timer.periodic(Duration(seconds: 1), (_) {
      count.value++;
    });

    onEffectCleanup(() => timer.cancel());
  });

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

Implementation

Effect call(void Function() effect,
    {bool lazy = false, JoltDebugFn? onDebug}) {
  return useAutoDispose(() => Effect(effect, lazy: lazy, onDebug: onDebug));
}