Skip to content

Add additional documentation for handling collections. Fix behavior of waitForCollectionCallback.#171

Merged
tgolen merged 4 commits into
mainfrom
marcaaron-mergeCollection
Aug 12, 2022
Merged

Add additional documentation for handling collections. Fix behavior of waitForCollectionCallback.#171
tgolen merged 4 commits into
mainfrom
marcaaron-mergeCollection

Conversation

@marcaaron

@marcaaron marcaaron commented Aug 11, 2022

Copy link
Copy Markdown
Contributor

Details

Fixes issue found here: Expensify/App#10051 (comment)

Semi-related to: https://github.com/Expensify/Expensify/issues/222064

Automated Tests

I can add some but the ones that exist are passing.

Linked PRs

Expensify/App#10373

@marcaaron marcaaron self-assigned this Aug 11, 2022
@marcaaron marcaaron requested a review from a team as a code owner August 11, 2022 00:16
@melvin-bot melvin-bot Bot requested review from dangrous and removed request for a team August 11, 2022 00:16
luacmartins
luacmartins previously approved these changes Aug 11, 2022

@luacmartins luacmartins left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM left a small NAB. Thanks for fixing the bug and adding more docs on collections!

Comment thread README.md Outdated

@dangrous dangrous left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Knowing very little about the context of this yet, this seems well written and understandable. Added a couple of places that could use clarification.

As for the actual content of it, I'll defer to someone who knows what they're talking about.

Comment thread README.md Outdated
Comment thread README.md Outdated
@dangrous

Copy link
Copy Markdown
Contributor

Updates look great to me! Resolves my questions perfectly.

luacmartins
luacmartins previously approved these changes Aug 11, 2022
@tgolen

tgolen commented Aug 12, 2022

Copy link
Copy Markdown
Collaborator

I was sort of holding off on reviewing this since @marcaaron and I were still having an ongoing discussion about whether a parameter is better or a separate method is better. Also, we should consider changing this param/method to be the default behavior for collections.

@marcaaron Did you want to make some of those changes in this PR?

@marcaaron

Copy link
Copy Markdown
Contributor Author

We can keep discussing and I can make them in a follow up (or just not at all). This PR largely just improves documentation and fixes a bug on main (and eventually staging).

Comment thread README.md Outdated
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report1.reportID}`, report1);
```

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a little more guidance here so people know which one to choose? I think adding information to say that when there are multiple keys, you should always use mergeCollect() because it optimizes all the UI updates. If it's a single key, it's fine to use either.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see you added all that information below. Maybe just add something here that says "See below for guidance on best practices"

Comment thread README.md Outdated
})(MyComponent);
```

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Comment thread README.md Outdated

```js
// Bad
_.each(reports, report => Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report)); // -> Connected component will update on each iteration

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_.each(reports, report => Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report)); // -> Connected component will update on each iteration
_.each(reports, report => Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report)); // -> A component using withOnyx() will have it's state updated with each iteration

Comment thread README.md Outdated
// Good
const values = {};
_.each(reports, report => values[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`] = report);
Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, values); // -> Connected component will update just once

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, values); // -> Connected component will update just once
Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, values); // -> A component using withOnyx() will only have it's state updated once

@marcaaron

Copy link
Copy Markdown
Contributor Author

Updated. Thanks for the notes @tgolen 🙇

@tgolen tgolen merged commit 6210505 into main Aug 12, 2022
@tgolen tgolen deleted the marcaaron-mergeCollection branch August 12, 2022 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants