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
+64Lines changed: 64 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -137,6 +137,70 @@ 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. `chat_42`).
There are several ways to subscribe to these keys:
173
+
174
+
```javascript
175
+
withOnyx({
176
+
allChats: {key:ONYXKEYS.COLLECTION.CHAT},
177
+
})(MyComponent);
178
+
```
179
+
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.
187
+
188
+
```js
189
+
Onyx.connect({
190
+
key:ONYXKEYS.COLLECTION.CHAT,
191
+
waitForCollectionCallback:true,
192
+
callback: (allChats) => {...}},
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 `Onyx.merge()` would re-render a connected component `n` times per key that you call it on.
203
+
140
204
## Clean up
141
205
142
206
To clear all data from `Onyx` we can use `Onyx.clear()`.
0 commit comments