Skip to content

Commit 0c78623

Browse files
authored
Merge pull request #169 from Expensify/tgolen-patch-1
Update README.md with some best-practices
2 parents 2017d79 + f95532c commit 0c78623

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,18 @@ Onyx.merge(ONYXKEYS.POLICY, {employeeList: ['Jack']}); // -> {employeeList: ['Ja
8686

8787
### Should I use `merge()` or `set()` or both?
8888

89-
- Use `merge()` if we want to merge partial data into an existing `Array` or `Object`
90-
- Use `set()` if we are working with simple values (`String`, `Boolean`, etc), need to completely overwrite a complex property of an `Object`, or reset some data entirely.
89+
- Use `merge()` when creating a new object
90+
- Use `merge()` to merge partial data into an existing object
91+
- Use `merge()` when storing simple values (`String`, `Boolean`, `Number`)
92+
- Use `set()` when you need to delete an Onyx key completely from storage
93+
- Use `set()` when you need to completely reset an object or array of data
9194

9295
Consecutive calls to `Onyx.merge()` with the same key are batched in a stack and processed in the order that they were called. This helps avoid race conditions where one merge possibly finishes before another. However, it's important to note that calls to `Onyx.set()` are not batched together with calls to `Onyx.merge()`. For this reason, it is usually preferable to use one or the other, but not both. Onyx is a work-in-progress so always test code to make sure assumptions are correct!
9396

97+
### Should I store things as an array or an object?
98+
99+
You should avoid arrays as much as possible. They do not work well with `merge()` because it can't update a single element in an array, it must always set the entire array each time. This forces you to use `set()` a lot, and as seen above, `merge()` is more performant and better to use in almost any situation. If you are working with an array of objects, then you should be using an Onyx collection because it's optimized for working with arrays of objects.
100+
94101
## Subscribing to data changes
95102

96103
To set up a basic subscription for a given key use the `Onyx.connect()` method.

0 commit comments

Comments
 (0)