Skip to content

Latest commit

 

History

History
46 lines (37 loc) · 1.2 KB

usePreviousDistinct.md

File metadata and controls

46 lines (37 loc) · 1.2 KB

usePreviousDistinct

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

Installation

dependencies:
  flutter_use: ^0.0.2

Usage

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'),
        ),
      ]
    );
  }
}

Reference

T? usePreviousDistinct<T>(T value, [bool Function(T, T)? compare])