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: API.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -76,6 +76,7 @@ Subscribes a react component's state directly to a store key
76
76
|[mapping.withOnyxInstance]| <code>Object</code> | whose setState() method will be called with any changed data This is used by React components to connect to Onyx |
77
77
|[mapping.callback]| <code>function</code> | a method that will be called with changed data This is used by any non-React code to connect to Onyx |
78
78
|[mapping.initWithStoredValues]| <code>Boolean</code> | If set to false, then no data will be prefilled into the component |
79
+
|[mapping.waitForCollectionCallback]| <code>Boolean</code> | If set to true, it will return the entire collection to the callback as a single object |
Copy file name to clipboardExpand all lines: README.md
+74Lines changed: 74 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -137,6 +137,80 @@ export default withOnyx({
137
137
138
138
It is preferable to use the HOC over `Onyx.connect()` in React code as `withOnyx()` will delay the rendering of the wrapped component until all keys have been accessed and made available.
139
139
140
+
## Collections
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. `report_42`).
There are several ways to subscribe to these keys:
173
+
174
+
```javascript
175
+
withOnyx({
176
+
allReports: {key:ONYXKEYS.COLLECTION.REPORT},
177
+
})(MyComponent);
178
+
```
179
+
180
+
This will add a prop to the component called `allReports` which is 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. The prop doesn't update on the initial rendering of the component until the entire collection has been read out of Onyx.
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
+
188
+
```js
189
+
Onyx.connect({
190
+
key:ONYXKEYS.COLLECTION.REPORT,
191
+
waitForCollectionCallback:true,
192
+
callback: (allReports) => {...}},
193
+
});
194
+
```
195
+
196
+
This final option forces `Onyx.connect()` to behave more like `withOnyx()` and only update the callback once with the entire collection initially and later with an updated version of the collection when individual keys update.
197
+
198
+
### Performance Considerations When Using Collections
199
+
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
+
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)); // -> A component using withOnyx() will have it's state updated with each iteration
0 commit comments