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

CPLAT-16997 Fix selector hooks redrawing when values haven't changed #730

Merged
merged 20 commits into from
Feb 3, 2022

Conversation

greglittlefield-wf
Copy link
Contributor

@greglittlefield-wf greglittlefield-wf commented Feb 3, 2022

Motivation

The useSelector hook and hooks created by createSelectorHook allow consumers to subscribe to store updates and rerender the component only when a value returned from the selector function changes.

However, the Dart versions of these APIs were always rerendering, even when the selected value didn't change.

This was caused by two issues:

  1. We were always wrapping the selected value in a new ReactInteropValue object, which meant that when you didn't provide a custom equality function, the default behavior on the JS side of comparing the value using === would always think the value changed
  2. We were always passing null into the hook as an equality function as opposed to conditionally calling it, which was causing null errors in React Redux JS that were getting ignored

Changes

  • Instead of always wrapping in an interop value, conditionally wrap only when necessary (Dart functions), just like we do in react-dart's JsBackedMap
  • Conditionally pass the equality function instead of always passing in null
  • Add regression tests to ensure renders only occur when we expect them to, for both hooks and connect
  • Add test coverage for values of different types, which also tests conditional wrapping of values
  • Add useSelector example

Release Notes

Review

See CONTRIBUTING.md for more details on review types (+1 / QA +1 / +10) and code review process.

Please review:

QA Checklist

  • Tests were updated and provide good coverage of the changeset and other affected code
  • Manual testing was performed if needed

Merge Checklist

While we perform many automated checks before auto-merging, some manual checks are needed:

  • A Client Platform member has reviewed these changes
  • There are no unaddressed comments - this check can be automated if reviewers use the "Request Changes" feature
  • For release PRs - Version metadata in Rosie comment is correct

@aviary-wf
Copy link

Security Insights

No security relevant content was detected by automated scans.

Action Items

  • Review PR for security impact; comment "security review required" if needed or unsure
  • Verify aviary.yaml coverage of security relevant code

Questions or Comments? Reach out on Slack: #support-infosec.

@@ -27,8 +27,7 @@ import 'package:over_react/over_react.dart';
/// list of children and pass that in as shown in the example below:
///
/// ```dart
/// import 'package:memoize/memoize.dart';
///
/// // imemo1 is from the `memoize` package
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed this to work around dependency_validator flagging imports in comments

@@ -497,14 +501,9 @@ class ReactJsReactReduxComponentFactoryProxy extends ReactJsContextComponentFact

/// Converts a Redux.dart [Store] into a Javascript object formatted for consumption by react-redux.
JsReactReduxStore _reduxifyStore(Store store) {
// Memoize this so that the same ReactInteropValue instances will be used
// for a given state, allowing JS `===` checks to not fail when the same
// state object is passed.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we pass through the value directly (and memoize it in the cases we wrap it in DartValueWrapper.wrapIfNeeded), this is no longer necessary

});
});
});
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These tests have moved to store_bindings_tests.dart. I started by refactoring them to use custom components so they could be run with multiple types of selected values, and they eventually morphed into tests that could also be shared with connect, thus the new file name.

@@ -0,0 +1,420 @@
// Copyright 2022 Workiva Inc.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm open to better name suggestions for this file

@rmconsole6-wk rmconsole6-wk changed the title Fix selector hooks redrawing when values haven't changed CPLAT-16997 Fix selector hooks redrawing when values haven't changed Feb 3, 2022
Copy link
Contributor

@aaronlademann-wf aaronlademann-wf left a comment

Choose a reason for hiding this comment

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

QA +1

Copy link
Contributor

@joebingham-wk joebingham-wk left a comment

Choose a reason for hiding this comment

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

+1, looks great! Those are some dope tests 🎉

Comment on lines +97 to +99
if (allValues.toSet().length != allValues.length) {
throw ArgumentError('initialValue and updatedValues must all be unique.');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Oohh this is clever. TIL

Copy link

@KealJones KealJones left a comment

Choose a reason for hiding this comment

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

+1

@greglittlefield-wf
Copy link
Contributor Author

@Workiva/release-management-p

Copy link

@rmconsole-wf rmconsole-wf left a comment

Choose a reason for hiding this comment

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

+1 from RM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants