Skip to content

Commit

Permalink
Merge branch 'master' into 4.0.0-wip
Browse files Browse the repository at this point in the history
# Conflicts:
#	lib/src/util/event_helpers.dart
#	test/over_react/util/event_helpers_test.dart
  • Loading branch information
sydneyjodon-wk committed Nov 24, 2020
2 parents 350c355 + 9d46abc commit 03d73c8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# OverReact Changelog

## [3.12.1](https://github.com/Workiva/over_react/compare/3.12.0...3.12.1)
- [#643] Use `propsOrStateMapsEqual` in `memo` so that function tearoffs don't cause unnecessary rerenders.

## [3.12.0](https://github.com/Workiva/over_react/compare/3.11.0...3.12.0)
- [#641] Expose new event helper APIs. In react-dart, using the `SyntheticEvent` class constructors were deprecated.
New event helpers were added as a replacement, and to make their usage convenient, these helpers have been exposed directly via OverReact.
Expand Down
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
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: over_react
version: 3.12.0
version: 3.12.1
description: A library for building statically-typed React UI components using Dart.
homepage: https://github.com/Workiva/over_react/
authors:
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.

2 changes: 1 addition & 1 deletion tools/analyzer_plugin/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
# Upon release, this should be pinned to the over_react version from ../../pubspec.yaml
# so that it always resolves to the same version of over_react that the user has pulled in,
# and thus has the same boilerplate parsing code that's running in the builder.
over_react: 3.12.0
over_react: 3.12.1
meta: ^1.1.6
path: ^1.5.1
source_span: ^1.7.0
Expand Down

0 comments on commit 03d73c8

Please sign in to comment.