Repository navigation
Description
Coming from this proposal
Background
The useOnyx hook (a wrapper around the original useOnyx) is used throughout the app. Inside it, we subscribe to SearchContext so the hook can return data from either the regular Onyx keys or the special SNAPSHOT keys (which are specifically designed for the Reports page).
However, useOnyx hooks that are not used on the Reports page don’t need SearchContext, since they don’t rely on currentSearchHash (which is only used to access the current SNAPSHOT data).
Problem
If SearchContext changes, then every useOnyx in the App will rerender. Such context change will trigger rerenders in screens that are rendered in the history, preloaded screens or modals that live outside of navigation like SearchRouter and can cause massive overhead.
Solution
Avoid useOnyx calls outside the Reports/Search tree from subscribing to SearchContext.
How:
SearchContext provides components with isOnSearch prop that with the help of SearchScopeProvider tells us if the useOnyx is part of Search/index component tree(is on Reports page).
To reduce rerenders, we could:
- take isOnSearch prop out of SearchContext and put it in SearchScopeProvider , isOnSearch doesn't trigger rerenders as it is a static prop.
- in useOnyx use the new use API to conditionally listen to SearchContext, that way useOnyx hooks that are part of Search/index component tree(this is static) will access SearchContext that they need and useOnyx hooks that don't need it won't rerender.
const useOnyx: OriginalUseOnyx = <TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey, options?: UseOnyxOptions<TKey, TReturnValue>, dependencies?: DependencyList) => {
const {isOnSearch} = useIsOnSearch();
let currentSearchHash: number | undefined;
if (isOnSearch) {
const {currentSearchHash: searchContextCurrentSearchHash} = use(SearchContext);
currentSearchHash = searchContextCurrentSearchHash;
}
... rest of useOnyx hook ...
Benefits
This will provide big boost to scenarios where SearchContext changes, examples(perf measured on iOS simulator):
- On Reports page the user changes search type with the search type menu(Submit -> Pay):
- OLD: total React work: 7234.014ms - ~55 commits
- NEW: total React work: 5357.51 - 48 commits (25%/1877 ms better)
- After fresh log into the app, user enters Reports page and then goes back to Inbox:
- OLD: total React work: 5992.427ms - ~67 commits
- NEW: total React work: 4890.688 - 59 commits (18%/1102 ms better)
Upwork Automation - Do Not Edit
- Upwork Job URL: https://www.upwork.com/jobs/~021965843867509223663
- Upwork Job ID: 1965843867509223663
- Last Price Increase: 2025-09-10
Issue Owner
Current Issue Owner: @jliexpensifyMetadata
Metadata
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
[-]Remove SearchContext from the useOnyx when not needed[/-][+][$250] Remove SearchContext from the useOnyx when not needed[/+]melvin-bot commentedon Sep 10, 2025
Job added to Upwork: https://www.upwork.com/jobs/~021965843867509223663
melvin-bot commentedon Sep 10, 2025
Triggered auto assignment to @jliexpensify (
Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details. Please add this bug to a GH project, as outlined in the SO.melvin-bot commentedon Sep 10, 2025
Triggered auto assignment to Contributor-plus team member for initial proposal review - @Pujan92 (
External)7 remaining items
[-][$250] Remove SearchContext from the useOnyx when not needed[/-][+][Due for payment 2025-09-26] [$250] Remove SearchContext from the useOnyx when not needed[/+]melvin-bot commentedon Sep 19, 2025
Reviewinglabel has been removed, please complete the "BugZero Checklist".melvin-bot commentedon Sep 19, 2025
The solution for this issue has been 🚀 deployed to production 🚀 in version 9.2.15-15 and is now subject to a 7-day regression period 📆. Here is the list of pull requests that resolve this issue:
If no regressions arise, payment will be issued on 2025-09-26. 🎊
For reference, here are some details about the assignees on this issue:
melvin-bot commentedon Sep 19, 2025
@Pujan92 @jliexpensify The PR fixing this issue has been merged! The following checklist (instructions) will need to be completed before the issue can be closed. Please copy/paste the BugZero Checklist from here into a new comment on this GH and complete it. If you have the K2 extension, you can simply click: [this button]
melvin-bot commentedon Sep 26, 2025
Payment Summary
Upwork Job
BugZero Checklist (@jliexpensify)
Pujan92 commentedon Sep 27, 2025
We don't need a checklist here as it was an improvement.
jliexpensify commentedon Sep 28, 2025
Payment Summary
JmillsExpensify commentedon Sep 30, 2025
$250 approved for @Pujan92