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
+2-8Lines changed: 2 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -81,10 +81,7 @@ This method will be deprecated soon. Please use `Onyx.connectWithoutView()` inst
81
81
| connectOptions.key | The Onyx key to subscribe to. |
82
82
| connectOptions.callback | A function that will be called when the Onyx data we are subscribed changes. |
83
83
| connectOptions.waitForCollectionCallback | If set to `true`, it will return the entire collection to the callback as a single object. |
84
-
| connectOptions.withOnyxInstance | The `withOnyx` class instance to be internally passed. **Only used inside `withOnyx()` HOC.**|
85
-
| connectOptions.statePropertyName | The name of the component's prop that is connected to the Onyx key. **Only used inside `withOnyx()` HOC.**|
86
-
| connectOptions.displayName | The component's display name. **Only used inside `withOnyx()` HOC.**|
87
-
| connectOptions.selector | This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook or `withOnyx()` HOC.** Using this setting on `useOnyx()` or `withOnyx()` can have very positive performance benefits because the component will only re-render when the subset of data changes. Otherwise, any change of data on any property would normally cause the component to re-render (and that can be expensive from a performance standpoint). |
84
+
| connectOptions.selector | This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook.** Using this setting on `useOnyx()` can have very positive performance benefits because the component will only re-render when the subset of data changes. Otherwise, any change of data on any property would normally cause the component to re-render (and that can be expensive from a performance standpoint). |
88
85
89
86
**Example**
90
87
```ts
@@ -107,10 +104,7 @@ Connects to an Onyx key given the options passed and listens to its changes.
107
104
| connectOptions.key | The Onyx key to subscribe to. |
108
105
| connectOptions.callback | A function that will be called when the Onyx data we are subscribed changes. |
109
106
| connectOptions.waitForCollectionCallback | If set to `true`, it will return the entire collection to the callback as a single object. |
110
-
| connectOptions.withOnyxInstance | The `withOnyx` class instance to be internally passed. **Only used inside `withOnyx()` HOC.**|
111
-
| connectOptions.statePropertyName | The name of the component's prop that is connected to the Onyx key. **Only used inside `withOnyx()` HOC.**|
112
-
| connectOptions.displayName | The component's display name. **Only used inside `withOnyx()` HOC.**|
113
-
| connectOptions.selector | This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook or `withOnyx()` HOC.** Using this setting on `useOnyx()` or `withOnyx()` can have very positive performance benefits because the component will only re-render when the subset of data changes. Otherwise, any change of data on any property would normally cause the component to re-render (and that can be expensive from a performance standpoint). |
107
+
| connectOptions.selector | This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook.** Using this setting on `useOnyx()` can have very positive performance benefits because the component will only re-render when the subset of data changes. Otherwise, any change of data on any property would normally cause the component to re-render (and that can be expensive from a performance standpoint). |
Copy file name to clipboardExpand all lines: README.md
+2-62Lines changed: 2 additions & 62 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -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 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()`.
12
+
2. Subscribe to changes of the data for a particular key or set of keys. React function components use the `useOnyx()` hook 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
@@ -136,7 +136,7 @@ To teardown the subscription call `Onyx.disconnect()` with the `connectionID` re
136
136
Onyx.disconnect(connectionID);
137
137
```
138
138
139
-
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.
139
+
We can also access values inside React function components via the `useOnyx()`[hook](https://react.dev/reference/react/hooks). When the data changes the component will re-render.
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.
193
-
194
-
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.
195
-
196
-
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.
// Second argument to funciton is `shouldDelayUpdates`
206
-
exportdefaultwithOnyx({
207
-
session: {
208
-
key:ONYXKEYS.SESSION
209
-
},
210
-
}, true)(App);
211
-
```
212
-
213
171
### Dependent Onyx Keys and useOnyx()
214
172
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.
215
173
@@ -246,24 +204,6 @@ export default App;
246
204
247
205
* 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.
248
206
249
-
**Detailed explanation of how this is handled and rendered with `withOnyx` HOC:**
250
-
1. The component mounts with a `reportID={1234}` prop
251
-
2.`withOnyx` evaluates the mapping
252
-
3.`withOnyx` connects to the key `reports_1234` because of the prop passed to the component
253
-
3.`withOnyx` connects to the key `policies_undefined` because `report` doesn't exist in the props yet, so the `policyID` defaults to `undefined`. * (see note below)
254
-
4. Onyx reads the data and updates the state of `withOnyx` with:
255
-
-`report={{reportID: 1234, policyID: 1, ... the rest of the object ...}}`
256
-
-`policy={undefined}` (since there is no policy with ID `undefined`)
257
-
5. There is still an `undefined` key in the mapping, so Onyx reads the data again
258
-
6. This time `withOnyx` connects to the key `policies_1` because the `report` object exists in the component's state and it has a `policyID: 1`
259
-
7. Onyx reads the data and updates the state of withOnyx with:
260
-
-`policy={{policyID: 1, ... the rest of the object ...}`
261
-
8. Now all mappings have values that are defined (not undefined) and the component is rendered with all necessary data
262
-
263
-
* It is VERY important to NOT use empty string default values like `report.policyID || ''`. This results in the key returned to `withOnyx` 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 `withOnyx` appropriately.
264
-
265
-
DO NOT use more than one `withOnyx` component at a time. It adds overhead and prevents some optimizations like batched rendering from working to its full potential.
266
-
267
207
It's also beneficial to use a [selector](https://github.com/Expensify/react-native-onyx/blob/main/API.md#connectmapping--number) with the mapping in case you need to grab a single item in a collection (like a single report action).
* @param connectOptions.key The Onyx key to subscribe to.
95
87
* @param connectOptions.callback A function that will be called when the Onyx data we are subscribed changes.
96
88
* @param connectOptions.waitForCollectionCallback If set to `true`, it will return the entire collection to the callback as a single object.
97
-
* @param connectOptions.withOnyxInstance The `withOnyx` class instance to be internally passed. **Only used inside `withOnyx()` HOC.**
98
-
* @param connectOptions.statePropertyName The name of the component's prop that is connected to the Onyx key. **Only used inside `withOnyx()` HOC.**
99
-
* @param connectOptions.displayName The component's display name. **Only used inside `withOnyx()` HOC.**
100
-
* @param connectOptions.selector This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook or `withOnyx()` HOC.**
101
-
* Using this setting on `useOnyx()` or `withOnyx()` can have very positive performance benefits because the component will only re-render
89
+
* @param connectOptions.selector This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook.**
90
+
* Using this setting on `useOnyx()` can have very positive performance benefits because the component will only re-render
102
91
* when the subset of data changes. Otherwise, any change of data on any property would normally
103
92
* cause the component to re-render (and that can be expensive from a performance standpoint).
104
93
* @returns The connection object to use when calling `Onyx.disconnect()`.
@@ -122,11 +111,8 @@ function connect<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKey>): Co
122
111
* @param connectOptions.key The Onyx key to subscribe to.
123
112
* @param connectOptions.callback A function that will be called when the Onyx data we are subscribed changes.
124
113
* @param connectOptions.waitForCollectionCallback If set to `true`, it will return the entire collection to the callback as a single object.
125
-
* @param connectOptions.withOnyxInstance The `withOnyx` class instance to be internally passed. **Only used inside `withOnyx()` HOC.**
126
-
* @param connectOptions.statePropertyName The name of the component's prop that is connected to the Onyx key. **Only used inside `withOnyx()` HOC.**
127
-
* @param connectOptions.displayName The component's display name. **Only used inside `withOnyx()` HOC.**
128
-
* @param connectOptions.selector This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook or `withOnyx()` HOC.**
129
-
* Using this setting on `useOnyx()` or `withOnyx()` can have very positive performance benefits because the component will only re-render
114
+
* @param connectOptions.selector This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook.**
115
+
* Using this setting on `useOnyx()` can have very positive performance benefits because the component will only re-render
130
116
* when the subset of data changes. Otherwise, any change of data on any property would normally
131
117
* cause the component to re-render (and that can be expensive from a performance standpoint).
132
118
* @returns The connection object to use when calling `Onyx.disconnect()`.
@@ -260,7 +246,6 @@ function multiSet(data: OnyxMultiSetInput): Promise<void> {
0 commit comments