react-native-onyx 1.0.102 → 1.0.104

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2601,6 +2601,15 @@ function getDisplayName(component) {
2601
2601
  return component.displayName || component.name || 'Component';
2602
2602
  }
2603
2603
 
2604
+ /**
2605
+ * Removes all the keys from state that are unrelated to the onyx data being mapped to the component.
2606
+ *
2607
+ * @param {Object} state of the component
2608
+ * @param {Object} onyxToStateMapping the object holding all of the mapping configuration for the component
2609
+ * @returns {Object}
2610
+ */
2611
+ const getOnyxDataFromState = (state, onyxToStateMapping) => underscore__WEBPACK_IMPORTED_MODULE_2___default().pick(state, underscore__WEBPACK_IMPORTED_MODULE_2___default().keys(onyxToStateMapping));
2612
+
2604
2613
  /* harmony default export */ function __WEBPACK_DEFAULT_EXPORT__(mapOnyxToState) {let shouldDelayUpdates = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
2605
2614
  // A list of keys that must be present in tempState before we can render the WrappedComponent
2606
2615
  const requiredKeysForInit = underscore__WEBPACK_IMPORTED_MODULE_2___default().chain(mapOnyxToState).
@@ -2673,16 +2682,20 @@ function getDisplayName(component) {
2673
2682
  this.checkEvictableKeys();
2674
2683
  }
2675
2684
 
2676
- componentDidUpdate(prevProps) {
2685
+ componentDidUpdate() {
2686
+ // When the state is passed to the key functions with Str.result(), omit anything
2687
+ // from state that was not part of the mapped keys.
2688
+ const onyxDataFromState = getOnyxDataFromState(this.state, mapOnyxToState);
2689
+
2677
2690
  // If any of the mappings use data from the props, then when the props change, all the
2678
2691
  // connections need to be reconnected with the new props
2679
- underscore__WEBPACK_IMPORTED_MODULE_2___default().each(mapOnyxToState, (mapping, propertyName) => {
2680
- const previousKey = _Str__WEBPACK_IMPORTED_MODULE_3__.result(mapping.key, prevProps);
2681
- const newKey = _Str__WEBPACK_IMPORTED_MODULE_3__.result(mapping.key, this.props);
2692
+ underscore__WEBPACK_IMPORTED_MODULE_2___default().each(mapOnyxToState, (mapping, propName) => {
2693
+ const previousKey = mapping.previousKey;
2694
+ const newKey = _Str__WEBPACK_IMPORTED_MODULE_3__.result(mapping.key, { ...this.props, ...onyxDataFromState });
2682
2695
  if (previousKey !== newKey) {
2683
2696
  _Onyx__WEBPACK_IMPORTED_MODULE_4__["default"].disconnect(this.activeConnectionIDs[previousKey], previousKey);
2684
2697
  delete this.activeConnectionIDs[previousKey];
2685
- this.connectMappingToOnyx(mapping, propertyName);
2698
+ this.connectMappingToOnyx(mapping, propName);
2686
2699
  }
2687
2700
  });
2688
2701
  this.checkEvictableKeys();
@@ -2691,9 +2704,8 @@ function getDisplayName(component) {
2691
2704
  componentWillUnmount() {
2692
2705
  // Disconnect everything from Onyx
2693
2706
  underscore__WEBPACK_IMPORTED_MODULE_2___default().each(mapOnyxToState, (mapping) => {
2694
- const key = _Str__WEBPACK_IMPORTED_MODULE_3__.result(mapping.key, this.props);
2695
- const connectionID = this.activeConnectionIDs[key];
2696
- _Onyx__WEBPACK_IMPORTED_MODULE_4__["default"].disconnect(connectionID, key);
2707
+ const key = _Str__WEBPACK_IMPORTED_MODULE_3__.result(mapping.key, { ...this.props, ...getOnyxDataFromState(this.state, mapOnyxToState) });
2708
+ _Onyx__WEBPACK_IMPORTED_MODULE_4__["default"].disconnect(this.activeConnectionIDs[key], key);
2697
2709
  });
2698
2710
  }
2699
2711
 
@@ -2825,7 +2837,14 @@ function getDisplayName(component) {
2825
2837
  * component
2826
2838
  */
2827
2839
  connectMappingToOnyx(mapping, statePropertyName) {
2828
- const key = _Str__WEBPACK_IMPORTED_MODULE_3__.result(mapping.key, this.props);
2840
+ const key = _Str__WEBPACK_IMPORTED_MODULE_3__.result(mapping.key, { ...this.props, ...getOnyxDataFromState(this.state, mapOnyxToState) });
2841
+
2842
+ // Remember the previous key so that if it ever changes, the component will reconnect to Onyx
2843
+ // in componentDidUpdate
2844
+ if (statePropertyName !== 'initialValue' && mapOnyxToState[statePropertyName]) {
2845
+ // eslint-disable-next-line no-param-reassign
2846
+ mapOnyxToState[statePropertyName].previousKey = key;
2847
+ }
2829
2848
 
2830
2849
  // eslint-disable-next-line rulesdir/prefer-onyx-connect-in-libs
2831
2850
  this.activeConnectionIDs[key] = _Onyx__WEBPACK_IMPORTED_MODULE_4__["default"].connect({