react-native-onyx 1.0.103 → 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.
package/lib/withOnyx.js CHANGED
@@ -20,6 +20,15 @@ function getDisplayName(component) {
20
20
  return component.displayName || component.name || 'Component';
21
21
  }
22
22
 
23
+ /**
24
+ * Removes all the keys from state that are unrelated to the onyx data being mapped to the component.
25
+ *
26
+ * @param {Object} state of the component
27
+ * @param {Object} onyxToStateMapping the object holding all of the mapping configuration for the component
28
+ * @returns {Object}
29
+ */
30
+ const getOnyxDataFromState = (state, onyxToStateMapping) => _.pick(state, _.keys(onyxToStateMapping));
31
+
23
32
  export default function (mapOnyxToState, shouldDelayUpdates = false) {
24
33
  // A list of keys that must be present in tempState before we can render the WrappedComponent
25
34
  const requiredKeysForInit = _.chain(mapOnyxToState)
@@ -92,16 +101,20 @@ export default function (mapOnyxToState, shouldDelayUpdates = false) {
92
101
  this.checkEvictableKeys();
93
102
  }
94
103
 
95
- componentDidUpdate(prevProps) {
104
+ componentDidUpdate() {
105
+ // When the state is passed to the key functions with Str.result(), omit anything
106
+ // from state that was not part of the mapped keys.
107
+ const onyxDataFromState = getOnyxDataFromState(this.state, mapOnyxToState);
108
+
96
109
  // If any of the mappings use data from the props, then when the props change, all the
97
110
  // connections need to be reconnected with the new props
98
- _.each(mapOnyxToState, (mapping, propertyName) => {
99
- const previousKey = Str.result(mapping.key, prevProps);
100
- const newKey = Str.result(mapping.key, this.props);
111
+ _.each(mapOnyxToState, (mapping, propName) => {
112
+ const previousKey = mapping.previousKey;
113
+ const newKey = Str.result(mapping.key, {...this.props, ...onyxDataFromState});
101
114
  if (previousKey !== newKey) {
102
115
  Onyx.disconnect(this.activeConnectionIDs[previousKey], previousKey);
103
116
  delete this.activeConnectionIDs[previousKey];
104
- this.connectMappingToOnyx(mapping, propertyName);
117
+ this.connectMappingToOnyx(mapping, propName);
105
118
  }
106
119
  });
107
120
  this.checkEvictableKeys();
@@ -110,9 +123,8 @@ export default function (mapOnyxToState, shouldDelayUpdates = false) {
110
123
  componentWillUnmount() {
111
124
  // Disconnect everything from Onyx
112
125
  _.each(mapOnyxToState, (mapping) => {
113
- const key = Str.result(mapping.key, this.props);
114
- const connectionID = this.activeConnectionIDs[key];
115
- Onyx.disconnect(connectionID, key);
126
+ const key = Str.result(mapping.key, {...this.props, ...getOnyxDataFromState(this.state, mapOnyxToState)});
127
+ Onyx.disconnect(this.activeConnectionIDs[key], key);
116
128
  });
117
129
  }
118
130
 
@@ -244,7 +256,14 @@ export default function (mapOnyxToState, shouldDelayUpdates = false) {
244
256
  * component
245
257
  */
246
258
  connectMappingToOnyx(mapping, statePropertyName) {
247
- const key = Str.result(mapping.key, this.props);
259
+ const key = Str.result(mapping.key, {...this.props, ...getOnyxDataFromState(this.state, mapOnyxToState)});
260
+
261
+ // Remember the previous key so that if it ever changes, the component will reconnect to Onyx
262
+ // in componentDidUpdate
263
+ if (statePropertyName !== 'initialValue' && mapOnyxToState[statePropertyName]) {
264
+ // eslint-disable-next-line no-param-reassign
265
+ mapOnyxToState[statePropertyName].previousKey = key;
266
+ }
248
267
 
249
268
  // eslint-disable-next-line rulesdir/prefer-onyx-connect-in-libs
250
269
  this.activeConnectionIDs[key] = Onyx.connect({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "1.0.103",
3
+ "version": "1.0.104",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",