Skip to content

Commit 9cc99cd

Browse files
committed
improve language in docs and add example
1 parent dfbe400 commit 9cc99cd

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ It is preferable to use the HOC over `Onyx.connect()` in React code as `withOnyx
139139

140140
## Collections
141141

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`).
143143

144144
```javascript
145145
const ONYXKEYS = {
146146
COLLECTION: {
147-
CHAT: 'chat_',
147+
REPORT: 'report_',
148148
},
149149
};
150150
```
@@ -154,16 +154,16 @@ const ONYXKEYS = {
154154
To save a new collection key we can either do:
155155

156156
```js
157-
Onyx.merge(`${ONYXKEYS.COLLECTION.CHAT}${chat1.id}`, chat1);
157+
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report1.reportID}`, report1);
158158
```
159159

160160
or we can set many at once with `mergeCollection()`:
161161

162162
```js
163-
Onyx.mergeCollection(ONYXKEYS.COLLECTION.CHAT, {
164-
[`${ONYXKEYS.COLLECTION.CHAT}${chat1.id}`]: chat1,
165-
[`${ONYXKEYS.COLLECTION.CHAT}${chat2.id}`]: chat2,
166-
[`${ONYXKEYS.COLLECTION.CHAT}${chat3.id}`]: chat3,
163+
Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, {
164+
[`${ONYXKEYS.COLLECTION.REPORT}${report1.reportID}`]: report1,
165+
[`${ONYXKEYS.COLLECTION.REPORT}${report2.reportID}`]: report2,
166+
[`${ONYXKEYS.COLLECTION.REPORT}${report3.reportID}`]: report3,
167167
});
168168
```
169169

@@ -173,23 +173,23 @@ There are several ways to subscribe to these keys:
173173

174174
```javascript
175175
withOnyx({
176-
allChats: {key: ONYXKEYS.COLLECTION.CHAT},
176+
allReports: {key: ONYXKEYS.COLLECTION.REPORT},
177177
})(MyComponent);
178178
```
179179

180180
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.
181181

182182
```js
183-
Onyx.connect({key: ONYXKEYS.COLLECTION.CHAT}, callback: (memberValue, memberKey) => {...}});
183+
Onyx.connect({key: ONYXKEYS.COLLECTION.REPORT}, callback: (memberValue, memberKey) => {...}});
184184
```
185185

186-
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.
187187

188188
```js
189189
Onyx.connect({
190-
key: ONYXKEYS.COLLECTION.CHAT,
190+
key: ONYXKEYS.COLLECTION.REPORT,
191191
waitForCollectionCallback: true,
192-
callback: (allChats) => {...}},
192+
callback: (allReports) => {...}},
193193
});
194194
```
195195

@@ -199,7 +199,17 @@ This final option forces `Onyx.connect()` to behave more like `withOnyx()` and o
199199

200200
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()`.
201201

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
207+
208+
// Good
209+
const values = {};
210+
_.each(reports, report => values[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`] = report);
211+
Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, values); // -> Connected component will update just once
212+
```
203213

204214
## Clean up
205215

0 commit comments

Comments
 (0)