react-native-onyx 2.0.44 → 2.0.45
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/README.md +99 -31
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ Awesome persistent storage solution wrapped in a Pub/Sub library.
|
|
|
9
9
|
- Onyx allows other code to subscribe to changes in data, and then publishes change events whenever data is changed
|
|
10
10
|
- Anything needing to read Onyx data needs to:
|
|
11
11
|
1. Know what key the data is stored in (for web, you can find this by looking in the JS console > Application > local storage)
|
|
12
|
-
2. Subscribe to changes of the data for a particular key or set of keys. React components use `withOnyx()` and non-React libs use `Onyx.connect()`.
|
|
12
|
+
2. Subscribe to changes of the data for a particular key or set of keys. React function components use the `useOnyx()` hook (recommended), both class and function components can use `withOnyx()` HOC (deprecated, not-recommended) and non-React libs use `Onyx.connect()`.
|
|
13
13
|
3. Get initialized with the current value of that key from persistent storage (Onyx does this by calling `setState()` or triggering the `callback` with the values currently on disk as part of the connection process)
|
|
14
14
|
- Subscribing to Onyx keys is done using a constant defined in `ONYXKEYS`. Each Onyx key represents either a collection of items or a specific entry in storage. For example, since all reports are stored as individual keys like `report_1234`, if code needs to know about all the reports (e.g. display a list of them in the nav menu), then it would subscribe to the key `ONYXKEYS.COLLECTION.REPORT`.
|
|
15
15
|
|
|
@@ -116,7 +116,41 @@ To teardown the subscription call `Onyx.disconnect()` with the `connectionID` re
|
|
|
116
116
|
Onyx.disconnect(connectionID);
|
|
117
117
|
```
|
|
118
118
|
|
|
119
|
-
We can also access values inside React components via the `withOnyx()` [higher order component](https://reactjs.org/docs/higher-order-components.html). When the data changes the component will re-render.
|
|
119
|
+
We can also access values inside React function components via the `useOnyx()` [hook](https://react.dev/reference/react/hooks) (recommended) or class and function components via the `withOnyx()` [higher order component](https://reactjs.org/docs/higher-order-components.html) (deprecated, not-recommended). When the data changes the component will re-render.
|
|
120
|
+
|
|
121
|
+
```javascript
|
|
122
|
+
import React from 'react';
|
|
123
|
+
import {useOnyx} from 'react-native-onyx';
|
|
124
|
+
|
|
125
|
+
const App = () => {
|
|
126
|
+
const [session] = useOnyx('session');
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<View>
|
|
130
|
+
{session.token ? <Text>Logged in</Text> : <Text>Logged out</Text>}
|
|
131
|
+
</View>
|
|
132
|
+
);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
export default App;
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
The `useOnyx()` hook won't delay the rendering of the component using it while the key/entity is being fetched and passed to the component. However, you can simulate this behavior by checking if the `status` of the hook's result metadata is `loading`. When `status` is `loading` it means that the Onyx data is being loaded into cache and thus is not immediately available, while `loaded` means that the data is already loaded and available to be consumed.
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
const [reports, reportsResult] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
|
|
142
|
+
const [session, sessionResult] = useOnyx(ONYXKEYS.SESSION);
|
|
143
|
+
|
|
144
|
+
if (reportsResult.status === 'loading' || sessionResult.status === 'loading') {
|
|
145
|
+
return <Placeholder />; // or `null` if you don't want to render anything.
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// rest of the component's code.
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
> [!warning]
|
|
152
|
+
> ## Deprecated Note
|
|
153
|
+
> Please note that the `withOnyx()` Higher Order Component (HOC) is now considered deprecated. Use `useOnyx()` hook instead.
|
|
120
154
|
|
|
121
155
|
```javascript
|
|
122
156
|
import React from 'react';
|
|
@@ -135,7 +169,7 @@ export default withOnyx({
|
|
|
135
169
|
})(App);
|
|
136
170
|
```
|
|
137
171
|
|
|
138
|
-
|
|
172
|
+
Differently from `useOnyx()`, `withOnyx()` will delay the rendering of the wrapped component until all keys/entities have been fetched and passed to the component, this can be convenient for simple cases. This however, can really delay your application if many entities are connected to the same component, you can pass an `initialValue` to each key to allow Onyx to eagerly render your component with this value.
|
|
139
173
|
|
|
140
174
|
```javascript
|
|
141
175
|
export default withOnyx({
|
|
@@ -146,7 +180,9 @@ export default withOnyx({
|
|
|
146
180
|
})(App);
|
|
147
181
|
```
|
|
148
182
|
|
|
149
|
-
Additionally, if your component has many keys/entities when your component will mount but will receive many updates as data is fetched from DB and passed down to it, as every key that gets fetched will trigger a `setState` on the `withOnyx` HOC. This might cause re-renders on the initial mounting, preventing the component from mounting/rendering in reasonable time, making your app feel slow and even delaying animations.
|
|
183
|
+
Additionally, if your component has many keys/entities when your component will mount but will receive many updates as data is fetched from DB and passed down to it, as every key that gets fetched will trigger a `setState` on the `withOnyx` HOC. This might cause re-renders on the initial mounting, preventing the component from mounting/rendering in reasonable time, making your app feel slow and even delaying animations.
|
|
184
|
+
|
|
185
|
+
You can workaround this by passing an additional object with the `shouldDelayUpdates` property set to true. Onyx will then put all the updates in a queue until you decide when then should be applied, the component will receive a function `markReadyForHydration`. A good place to call this function is on the `onLayout` method, which gets triggered after your component has been rendered.
|
|
150
186
|
|
|
151
187
|
```javascript
|
|
152
188
|
const App = ({session, markReadyForHydration}) => (
|
|
@@ -164,27 +200,43 @@ export default withOnyx({
|
|
|
164
200
|
}, true)(App);
|
|
165
201
|
```
|
|
166
202
|
|
|
167
|
-
### Dependent Onyx Keys and
|
|
203
|
+
### Dependent Onyx Keys and useOnyx()
|
|
168
204
|
Some components need to subscribe to multiple Onyx keys at once and sometimes, one key might rely on the data from another key. This is similar to a JOIN in SQL.
|
|
169
205
|
|
|
170
206
|
Example: To get the policy of a report, the `policy` key depends on the `report` key.
|
|
171
207
|
|
|
172
208
|
```javascript
|
|
173
|
-
|
|
174
|
-
report
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
209
|
+
const App = ({reportID}) => {
|
|
210
|
+
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
|
|
211
|
+
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`);
|
|
212
|
+
|
|
213
|
+
return (
|
|
214
|
+
<View>
|
|
215
|
+
{/* Render with policy data */}
|
|
216
|
+
</View>
|
|
217
|
+
);
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
export default App;
|
|
181
221
|
```
|
|
182
222
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
223
|
+
**Detailed explanation of how this is handled and rendered with `useOnyx()`:**
|
|
224
|
+
|
|
225
|
+
1. The component mounts with a `reportID={1234}` prop.
|
|
226
|
+
2. The `useOnyx` hook evaluates the mapping and subscribes to the key `reports_1234` using the `reportID` prop.
|
|
227
|
+
3. The `useOnyx` hook fetches the data for the key `reports_1234` from Onyx and sets the state with the initial value (if provided).
|
|
228
|
+
4. Since `report` is not defined yet, `report?.policyID` defaults to `undefined`. The `useOnyx` hook subscribes to the key `policies_undefined`.
|
|
229
|
+
5. The `useOnyx` hook reads the data and updates the state of the component:
|
|
230
|
+
- `report={{reportID: 1234, policyID: 1, ...rest of the object...}}`
|
|
231
|
+
- `policy={undefined}` (since there is no policy with ID `undefined`)
|
|
232
|
+
6. The `useOnyx` hook again evaluates the key `policies_1` after fetching the updated `report` object which has `policyID: 1`.
|
|
233
|
+
7. The `useOnyx` hook reads the data and updates the state with:
|
|
234
|
+
- `policy={{policyID: 1, ...rest of the object...}}`
|
|
235
|
+
8. Now, all mappings have values that are defined (not undefined), and the component is rendered with all necessary data.
|
|
236
|
+
|
|
237
|
+
* It is VERY important to NOT use empty string default values like `report.policyID || ''`. This results in the key returned to `useOnyx` as `policies_`, which subscribes to the ENTIRE POLICY COLLECTION and is most assuredly not what you were intending. You can use a default of `0` (as long as you are reasonably sure that there is never a policyID=0). This allows Onyx to return `undefined` as the value of the policy key, which is handled by `useOnyx` appropriately.
|
|
186
238
|
|
|
187
|
-
**Detailed explanation of how this is handled and rendered:**
|
|
239
|
+
**Detailed explanation of how this is handled and rendered with `withOnyx` HOC:**
|
|
188
240
|
1. The component mounts with a `reportID={1234}` prop
|
|
189
241
|
2. `withOnyx` evaluates the mapping
|
|
190
242
|
3. `withOnyx` connects to the key `reports_1234` because of the prop passed to the component
|
|
@@ -239,15 +291,23 @@ Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, {
|
|
|
239
291
|
There are several ways to subscribe to these keys:
|
|
240
292
|
|
|
241
293
|
```javascript
|
|
242
|
-
|
|
243
|
-
allReports
|
|
244
|
-
|
|
294
|
+
const MyComponent = () => {
|
|
295
|
+
const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
|
|
296
|
+
|
|
297
|
+
return (
|
|
298
|
+
<View>
|
|
299
|
+
{/* Render with allReports data */}
|
|
300
|
+
</View>
|
|
301
|
+
);
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
export default MyComponent;
|
|
245
305
|
```
|
|
246
306
|
|
|
247
307
|
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.
|
|
248
308
|
|
|
249
309
|
```js
|
|
250
|
-
Onyx.connect({key: ONYXKEYS.COLLECTION.REPORT}, callback: (memberValue, memberKey) => {...}
|
|
310
|
+
Onyx.connect({key: ONYXKEYS.COLLECTION.REPORT}, callback: (memberValue, memberKey) => {...});
|
|
251
311
|
```
|
|
252
312
|
|
|
253
313
|
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.
|
|
@@ -256,11 +316,11 @@ This will fire the callback once per member key depending on how many collection
|
|
|
256
316
|
Onyx.connect({
|
|
257
317
|
key: ONYXKEYS.COLLECTION.REPORT,
|
|
258
318
|
waitForCollectionCallback: true,
|
|
259
|
-
callback: (allReports) => {...}
|
|
319
|
+
callback: (allReports) => {...},
|
|
260
320
|
});
|
|
261
321
|
```
|
|
262
322
|
|
|
263
|
-
This final option forces `Onyx.connect()` to behave more like `
|
|
323
|
+
This final option forces `Onyx.connect()` to behave more like `useOnyx()` and only update the callback once with the entire collection initially and later with an updated version of the collection when individual keys update.
|
|
264
324
|
|
|
265
325
|
### Performance Considerations When Using Collections
|
|
266
326
|
|
|
@@ -270,12 +330,12 @@ Remember, `mergeCollection()` will notify a subscriber only *once* with the tota
|
|
|
270
330
|
|
|
271
331
|
```js
|
|
272
332
|
// Bad
|
|
273
|
-
_.each(reports, report => Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report)); // -> A component using
|
|
333
|
+
_.each(reports, report => Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report)); // -> A component using useOnyx() will have it's state updated with each iteration
|
|
274
334
|
|
|
275
335
|
// Good
|
|
276
336
|
const values = {};
|
|
277
337
|
_.each(reports, report => values[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`] = report);
|
|
278
|
-
Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, values); // -> A component using
|
|
338
|
+
Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, values); // -> A component using useOnyx() will only have its state updated once
|
|
279
339
|
```
|
|
280
340
|
|
|
281
341
|
## Clean up
|
|
@@ -325,12 +385,20 @@ Onyx.init({
|
|
|
325
385
|
```
|
|
326
386
|
|
|
327
387
|
```js
|
|
328
|
-
|
|
329
|
-
reportActions
|
|
330
|
-
|
|
331
|
-
canEvict:
|
|
332
|
-
|
|
333
|
-
|
|
388
|
+
const ReportActionsView = ({reportID, isActiveReport}) => {
|
|
389
|
+
const [reportActions] = useOnyx(
|
|
390
|
+
`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}_`,
|
|
391
|
+
{canEvict: () => !isActiveReport}
|
|
392
|
+
);
|
|
393
|
+
|
|
394
|
+
return (
|
|
395
|
+
<View>
|
|
396
|
+
{/* Render with reportActions data */}
|
|
397
|
+
</View>
|
|
398
|
+
);
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
export default ReportActionsView;
|
|
334
402
|
```
|
|
335
403
|
|
|
336
404
|
# Benchmarks
|