You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+23-13Lines changed: 23 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -139,12 +139,12 @@ It is preferable to use the HOC over `Onyx.connect()` in React code as `withOnyx
139
139
140
140
## Collections
141
141
142
-
Collections allow keys with similar value types to be subscribed together by subscribing to the collection key. To define one, it must be included in the `ONYXKEYS.COLLECTION` object and it must be suffixed with an underscore. Member keys should use a unique identifier or index after the collection key prefix (e.g. `chat_42`).
142
+
Collections allow keys with similar value types to be subscribed together by subscribing to the collection key. To define one, it must be included in the `ONYXKEYS.COLLECTION` object and it must be suffixed with an underscore. Member keys should use a unique identifier or index after the collection key prefix (e.g. `report_42`).
@@ -173,23 +173,23 @@ There are several ways to subscribe to these keys:
173
173
174
174
```javascript
175
175
withOnyx({
176
-
allChats: {key:ONYXKEYS.COLLECTION.CHAT},
176
+
allReports: {key:ONYXKEYS.COLLECTION.REPORT},
177
177
})(MyComponent);
178
178
```
179
179
180
180
This will return the initial collection as an object of collection member key/values. Changes to the individual member keys will modify the entire object and new props will be passed with each individual key update.
This will fire the callback `n` times depending on how many collection member keys have been stored. Changes to those keys after the initial callbacks fire will occur when each individual key is updated.
186
+
This will fire the callback once per member key depending on how many collection member keys are currently stored. Changes to those keys after the initial callbacks fire will occur when each individual key is updated.
187
187
188
188
```js
189
189
Onyx.connect({
190
-
key:ONYXKEYS.COLLECTION.CHAT,
190
+
key:ONYXKEYS.COLLECTION.REPORT,
191
191
waitForCollectionCallback:true,
192
-
callback: (allChats) => {...}},
192
+
callback: (allReports) => {...}},
193
193
});
194
194
```
195
195
@@ -199,7 +199,17 @@ This final option forces `Onyx.connect()` to behave more like `withOnyx()` and o
199
199
200
200
Be cautious when using collections as things can get out of hand if you have a subscriber hooked up to a collection key that has large numbers of individual keys. If this is the case, it is critical to use `mergeCollection()` over `merge()`.
201
201
202
-
Remember, `mergeCollection()` will notify a subscriber only *once* with the total collected values whereas each call to `Onyx.merge()` would re-render a connected component `n` times per key that you call it on.
202
+
Remember, `mergeCollection()` will notify a subscriber only *once* with the total collected values whereas each call to `merge()` would re-render a connected component *each time it is called*. Consider this example where `reports` is an array of reports that we want to index and save.
203
+
204
+
```js
205
+
// Bad
206
+
_.each(reports, report=>Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report)); // -> Connected component will update on each iteration
0 commit comments