Just like usePrevious
but it will only update once the value actually changes. This is important when other hooks are involved and you aren't just interested in the previous props version, but want to know the previous distinct value
dependencies:
flutter_use: ^0.0.2
class Sample extends HookWidget {
@override
Widget build(BuildContext context) {
final count = useCounter(0);
final unrelatedCount = useCounter(0);
final prevCount = usePreviousDistinct(count.value);
return Column(
children: [
Text("Now: ${count.value}, before: $prevCount"),
ElevatedButton(
onPressed: () => count.inc(),
child: const Text('Increment'),
),
Text("Unrelated: ${unrelatedCount.value}"),
ElevatedButton(
onPressed: () => unrelatedCount.inc(),
child: const Text('Increment Unrelated'),
),
]
);
}
}
T? usePreviousDistinct<T>(T value, [bool Function(T, T)? compare])