Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mimic React Hook's dependencies comparison #374

Merged
merged 5 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/flutter_hooks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased minor
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved

- **Breaking**: `keys` comparison has changed under the following conditions:
- Change from `double.nan` to `double.nan` preserves the state.
- Change from `0.0` to `-0.0` or vice versa does NOT preserve the state.

## 0.19.0 - 2023-07-10

- Added `useSearchController` (thanks to @snapsl)
Expand Down
22 changes: 21 additions & 1 deletion packages/flutter_hooks/lib/src/framework.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ Calling them outside of build method leads to an unstable state and is therefore
///
/// - `hook1.keys == hook2.keys` (typically if the list is immutable)
/// - If there's any difference in the content of [Hook.keys], using `operator==`.
///
/// There are exceptions when comparing [Hook.keys] before using `operator==`:
/// - A state is preserved when one of the [Hook.keys] is [double.nan].
/// - A state is NOT preserved when one of the [Hook.keys] is changed from 0.0 to -0.0.
static bool shouldPreserveState(Hook<Object?> hook1, Hook<Object?> hook2) {
final p1 = hook1.keys;
final p2 = hook2.keys;
Expand All @@ -172,7 +176,23 @@ Calling them outside of build method leads to an unstable state and is therefore
if (!i1.moveNext() || !i2.moveNext()) {
return true;
}
if (i1.current != i2.current) {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs need to be updated to match

final curr1 = i1.current;
final curr2 = i2.current;

if (curr1 is num && curr2 is num) {
// Checks if both are NaN
if (curr1.isNaN && curr2.isNaN) {
return true;
}

// Checks if one is 0.0 and the other is -0.0
if (curr1 == 0 && curr2 == 0) {
return curr1.isNegative == curr2.isNegative;
}
}

if (curr1 != curr2) {
return false;
}
}
Expand Down
49 changes: 49 additions & 0 deletions packages/flutter_hooks/test/use_effect_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,55 @@ void main() {
verifyNoMoreInteractions(disposerA);
verifyNoMoreInteractions(effect);
});

testWidgets(
'does NOT call effect when one of keys is NaN and others are same',
(tester) async {
parameters = [double.nan];
await tester.pumpWidget(builder());

verifyInOrder([
effect(),
unrelated(),
]);
verifyNoMoreInteractions(effect);

parameters = [double.nan];
await tester.pumpWidget(builder());

verifyNoMoreInteractions(effect);
});

testWidgets(
'calls effect when one of keys is changed from 0.0 to -0.0 and vice versa',
(tester) async {
parameters = [0.0];
await tester.pumpWidget(builder());

verifyInOrder([
effect(),
unrelated(),
]);
verifyNoMoreInteractions(effect);

parameters = [-0.0];
await tester.pumpWidget(builder());

verifyInOrder([
effect(),
unrelated(),
]);
verifyNoMoreInteractions(effect);

parameters = [0.0];
await tester.pumpWidget(builder());

verifyInOrder([
effect(),
unrelated(),
]);
verifyNoMoreInteractions(effect);
});
}

class MockEffect extends Mock {
Expand Down