Skip to content

Commit

Permalink
Make useCallback hook's keys optional (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
jezsung authored Jul 10, 2023
1 parent 2a6b45c commit 81c7833
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 39 deletions.
1 change: 1 addition & 0 deletions packages/flutter_hooks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased minor

- Added `useSearchController` (thanks to @snapsl)
- Keys on `useCallback` are now optional, to match `useMemoized` (thanks to @jezsung)

## 0.18.6

Expand Down
6 changes: 3 additions & 3 deletions packages/flutter_hooks/lib/src/primitives.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ ObjectRef<T> useRef<T>(T initialValue) {
/// }, [key]);
/// ```
T useCallback<T extends Function>(
T callback,
List<Object?> keys,
) {
T callback, [
List<Object?> keys = const <Object>[],
]) {
return useMemoized(() => callback, keys);
}

Expand Down
36 changes: 0 additions & 36 deletions packages/flutter_hooks/test/memoized_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,42 +65,6 @@ void main() {
reason: 'The ref value has the last assigned value.');
});

testWidgets('useCallback', (tester) async {
late int Function() fn;

await tester.pumpWidget(
HookBuilder(builder: (context) {
fn = useCallback<int Function()>(() => 42, []);
return Container();
}),
);

expect(fn(), 42);

late int Function() fn2;

await tester.pumpWidget(
HookBuilder(builder: (context) {
fn2 = useCallback<int Function()>(() => 42, []);
return Container();
}),
);

expect(fn2, fn);

late int Function() fn3;

await tester.pumpWidget(
HookBuilder(builder: (context) {
fn3 = useCallback<int Function()>(() => 21, [42]);
return Container();
}),
);

expect(fn3, isNot(fn));
expect(fn3(), 21);
});

testWidgets('memoized without parameter calls valueBuilder once',
(tester) async {
late int result;
Expand Down
72 changes: 72 additions & 0 deletions packages/flutter_hooks/test/use_callback_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

import 'mock.dart';

void main() {
testWidgets('useCallback', (tester) async {
late int Function() fn;

await tester.pumpWidget(
HookBuilder(builder: (context) {
fn = useCallback<int Function()>(() => 42, []);
return Container();
}),
);

expect(fn(), 42);

late int Function() fn2;

await tester.pumpWidget(
HookBuilder(builder: (context) {
fn2 = useCallback<int Function()>(() => 42, []);
return Container();
}),
);

expect(fn2, fn);

late int Function() fn3;

await tester.pumpWidget(
HookBuilder(builder: (context) {
fn3 = useCallback<int Function()>(() => 21, [42]);
return Container();
}),
);

expect(fn3, isNot(fn));
expect(fn3(), 21);
});

testWidgets(
'should return same function when keys are not specified',
(tester) async {
late Function fn1;
late Function fn2;

await tester.pumpWidget(
HookBuilder(
key: const Key('hook_builder'),
builder: (context) {
fn1 = useCallback(() {});
return Container();
},
),
);

await tester.pumpWidget(
HookBuilder(
key: const Key('hook_builder'),
builder: (context) {
fn2 = useCallback(() {});
return Container();
},
),
);

expect(fn1, same(fn2));
},
);
}

0 comments on commit 81c7833

Please sign in to comment.