useSignalEffect function
void
useSignalEffect(})
Creates a new reactive effect and registers it with the widget's lifecycle.
The effect is automatically created on build and is disposed of when the widget is unmounted.
Any reactive signal accessed inside the callback cb is tracked as a dependency, and the effect
is executed automatically whenever those signals change.
Parameters
cb: The effect callback to execute when tracked dependencies change.keys: A list of objects to watch. If any key changes, the effect is disposed and re-created.onDispose: An optional callback executed when the effect itself is disposed.debugLabel: An optional debug label to identify the effect in developer tools.
Example
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:signals_hooks/signals_hooks.dart';
class LoggerWidget extends HookWidget {
const LoggerWidget({super.key});
@override
Widget build(BuildContext context) {
final count = useSignal(0);
// Set up a side effect to log value changes
useSignalEffect(
() {
debugPrint('The counter value is now: ${count.value}');
},
keys: [count],
onDispose: () => debugPrint('Logger effect disposed'),
);
return TextButton(
onPressed: () => count.value++,
child: Text('Count: ${count.value}'),
);
}
}
Implementation
void useSignalEffect(
dynamic Function() cb, {
/// A list of objects to watch for changes.
///
/// If any of the keys change, the effect will be re-created.
List<Object?> keys = const <Object>[],
/// A callback that is executed when the effect is disposed.
dynamic Function()? onDispose,
/// The debug label for the effect.
String? debugLabel,
}) {
useEffect(
() => effect(cb, onDispose: onDispose, debugLabel: debugLabel),
keys,
);
return;
}