useLinkedSignal<T, S> function
LinkedSignal<T, S>
useLinkedSignal<T, S>(
- S source(), {
- List<
Object?> keys = const <Object>[], - LinkedSignalOptions<
T, S> ? options,
Creates a new LinkedSignal that resets its value whenever its source computation changes.
A LinkedSignal is a specialized signal that "links" to another reactive source or computed value, automatically updating or resetting itself based on a custom computation function when the source changes.
Parameters
source: A computation function that yields the source value of typeS.options: Configuration options for the linked signal, detailing how the source valueSmaps to the signal's inner valueT.keys: A list of objects to watch. If any key changes, the signal is re-created.
Example
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:signals_hooks/signals_hooks.dart';
class ProfileEditor extends HookWidget {
final String initialUsername;
const ProfileEditor({required this.initialUsername, super.key});
@override
Widget build(BuildContext context) {
// LinkedSignal resets to initialUsername if the prop initialUsername changes,
// but allows local modifications in the meantime.
final usernameSignal = useLinkedSignal(
() => initialUsername,
keys: [initialUsername],
options: LinkedSignalOptions(
computation: (source, previous) => source,
),
);
return Column(
children: [
TextField(
controller: useTextEditingController(text: usernameSignal.value)
..addListener(() {
// Local modifications allowed
usernameSignal.value = usernameSignal.value;
}),
),
Text('Live Username Signal: ${usernameSignal.value}'),
],
);
}
}
Implementation
LinkedSignal<T, S> useLinkedSignal<T, S>(
S Function() source, {
List<Object?> keys = const <Object>[],
LinkedSignalOptions<T, S>? options,
}) {
final s = useMemoized(
() => linkedSignal<T, S>(source, options: options),
keys,
);
return useExistingSignal(s, keys: keys);
}