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

Use propsOrStateMapsEqual in memo so that tearoffs don't cause unnecessary rerenders #643

Merged
merged 2 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 6 additions & 5 deletions lib/src/util/memo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
library over_react.memo;

import 'package:over_react/src/component_declaration/component_type_checking.dart';
import 'package:over_react/src/util/equality.dart';
import 'package:react/react_client/react_interop.dart' as react_interop;
import 'package:react/react_client.dart';
import 'package:over_react/component_base.dart';
Expand All @@ -32,25 +33,25 @@ import 'package:over_react/component_base.dart';
/// ```dart
/// import 'package:over_react/over_react.dart';
///
/// UiFactory<UiProps> MemoExample = memo<UiProps>(uiFunction(
/// UiFactory<UiProps> MemoExample = memo(uiFunction(
/// (props) {
/// // render using props
/// },
/// UiFactoryConfig(displayName: 'MemoExample'),
/// $MemoExampleConfig, // ignore: undefined_identifier
/// ));
/// ```
///
/// `memo` only affects props changes. If your function component wrapped in `memo` has a
/// `useState` or `useContext` Hook in its implementation, it will still rerender when `state` or `context` change.
///
/// By default it will only shallowly compare complex objects in the props map.
/// By default it will only shallowly compare complex objects in the props map using [propsOrStateMapsEqual].
/// If you want control over the comparison, you can also provide a custom comparison
/// function to the [areEqual] argument as shown in the example below.
///
/// ```dart
/// import 'package:over_react/over_react.dart';
///
/// UiFactory<MemoWithComparisonProps> MemoWithComparison = memo<MemoWithComparisonProps>(uiFunction(
/// UiFactory<MemoWithComparisonProps> MemoWithComparison = memo(uiFunction(
/// (props) {
/// // render using props
/// },
Expand Down Expand Up @@ -80,7 +81,7 @@ UiFactory<TProps> memo<TProps extends UiProps>(UiFactory<TProps> factory,

hoc = react_interop.memo2(factory().componentFactory, areEqual: wrapProps);
} else {
hoc = react_interop.memo2(factory().componentFactory);
hoc = react_interop.memo2(factory().componentFactory, areEqual: propsOrStateMapsEqual);
}

setComponentTypeMeta(hoc,
Expand Down
19 changes: 19 additions & 0 deletions test/over_react/component/memo_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ part 'memo_test.over_react.g.dart';
int renderCount = 0;
mixin FunctionCustomPropsProps on UiProps {
int testProp;
Function() testFuncProp;
}

UiFactory<FunctionCustomPropsProps> FunctionCustomProps = uiFunction(
Expand Down Expand Up @@ -84,6 +85,24 @@ main() {
expect(renderCount, equals(1));
});

// Asserts that propsOrStateMapsEqual is being used for the equality check
// when the consumer does not provide a custom `areEqual` parameter.
test('memoizes properly when a tear-off is passed as a function prop value', () {
renderCount = 0;
UiFactory<FunctionCustomPropsProps> FunctionCustomPropsMemo = memo(FunctionCustomProps);
void handleTestFunc() {}
void handleTestFunc2() {}
final testJacket = mount((FunctionCustomPropsMemo()..testFuncProp = handleTestFunc)());
testJacket.rerender((FunctionCustomPropsMemo()..testFuncProp = handleTestFunc)());
testJacket.rerender((FunctionCustomPropsMemo()..testFuncProp = handleTestFunc)());

expect(renderCount, equals(1), reason: 'An identical tear-off value should not result in a re-render');

testJacket.rerender((FunctionCustomPropsMemo()..testFuncProp = handleTestFunc2)());

expect(renderCount, equals(2), reason: 'A different tear-off value should result in a re-render');
});

test('memoizes based on areEqual parameter', () {
renderCount = 0;
UiFactory<FunctionCustomPropsProps> FunctionCustomPropsMemo =
Expand Down
17 changes: 15 additions & 2 deletions test/over_react/component/memo_test.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.