Refactor: isolate getAllTaxRates from ONYXKEYS.COLLECTION.POLICY Onyx.connect data#75024
Conversation
|
@ikevin127 Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button] |
| return []; | ||
| } | ||
| return policyIDs.map((policyID) => policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]).filter((policy) => !!policy); | ||
| }, [policyIDs, policies]); |
There was a problem hiding this comment.
❌ PERF-6 (docs)
Passing the entire policies collection as a dependency causes this useMemo to re-execute whenever any property of any policy changes, even if the policies referenced by policyIDs haven't changed. This is unnecessary since you're only accessing specific policies by their IDs.
Suggested fix:
Instead of depending on the entire policies object, create a selector or extract only the specific policies needed:
const selectedPolicies = useMemo(() => {
if (policyIDs.length === 0) {
return [];
}
return policyIDs.map((policyID) => {
const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
return policy ? {id: policy.id, taxRates: policy.taxRates} : null;
}).filter((policy) => !!policy);
}, [policyIDs, ...policyIDs.map(id => policies?.[`${ONYXKEYS.COLLECTION.POLICY}${id}`])]);However, this may be complex. Consider using a useOnyx selector approach for the specific policy IDs instead.
| const [currentUserLogin] = useOnyx(ONYXKEYS.SESSION, {selector: emailSelector, canBeMissing: false}); | ||
| const [currentUserAccountID = -1] = useOnyx(ONYXKEYS.SESSION, {selector: accountIDSelector, canBeMissing: false}); | ||
|
|
||
| const taxRates = getAllTaxRates(policies); |
There was a problem hiding this comment.
❌ PERF-4 (docs)
The taxRates object is not memoized but is used as a dependency in the taxAutocompleteList useMemo (line 291). Since getAllTaxRates() returns a new object on every render, the taxAutocompleteList will be recalculated on every render, defeating the purpose of memoization.
Suggested fix:
const taxRates = useMemo(() => getAllTaxRates(policies), [policies]);This ensures referential stability and allows the downstream useMemo to work correctly.
There was a problem hiding this comment.
Is it really necessary as React compiler should do this automatically?
|
PR doesn’t need product input as a refactor PR. Unassigning and unsubscribing myself. |
Reviewer Checklist
Screenshots/VideosScreen.Recording.2025-11-13.at.15.46.27.mov |
|
@shubham1206agra Found this issue by generating testing steps with AI - noting that the issue is present on staging as well - but it is related to the logic we're working on in this PR: Precondition:
issue.mov |
|
✅ Reviewed - checklist completed. Awaiting for comments to be addressed before approving 💯 |
@ikevin127 This behavior looks as expected. |
tgolen
left a comment
There was a problem hiding this comment.
Can you please see if updating this branch with main will fix the ESLint errors? And if it doesn't, then I think either the imports should be fixed, or at least have the ESLint error disabled on those lines.
|
🚀 Deployed to staging by https://github.com/tgolen in version: 9.2.61-0 🚀
|
|
🚀 Deployed to production by https://github.com/grgia in version: 9.2.61-5 🚀
|
Explanation of Change
Fixed Issues
$ #66397
Tests
Offline tests
Same as Tests
QA Steps
Same as Tests
PR Author Checklist
### Fixed Issuessection aboveTestssectionOffline stepssectionQA stepssectioncanBeMissingparam foruseOnyxtoggleReportand notonIconClick)src/languages/*files and using the translation methodSTYLE.md) were followedAvatar, I verified the components usingAvatarare working as expected)StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))npm run compress-svg)Avataris modified, I verified thatAvataris working as expected in all cases)Designlabel and/or tagged@Expensify/designso the design team can review the changes.ScrollViewcomponent to make it scrollable when more elements are added to the page.mainbranch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTeststeps.Screenshots/Videos
Screen.Recording.2025-11-12.at.10.32.36.PM.mov