-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[NoQA] Remove useDeepCompareRef hook #78377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
roryabraham
merged 11 commits into
Expensify:main
from
callstack-internal:callstack-internal/szymonzalarski/get-rid-of-use-deep-compare-ref
Feb 9, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6c83154
Remove useDeepCompareRef hook
szymonzalarski98 983a764
PR fixes
szymonzalarski98 de46d6c
Revert useDeepCompareRef hook
szymonzalarski98 8aab326
Merge branch 'main' into callstack-internal/szymonzalarski/get-rid-of…
szymonzalarski98 1cc3f27
Merge branch 'main' into callstack-internal/szymonzalarski/get-rid-of…
szymonzalarski98 e966500
PR fixes
szymonzalarski98 8de6d8c
Update useDeepCompareRef docs
szymonzalarski98 259948a
Fix useArchivedReportsIdSet and add a comment
szymonzalarski98 34d2774
Merge branch 'main' into callstack-internal/szymonzalarski/get-rid-of…
szymonzalarski98 1219184
Prettier fix
szymonzalarski98 088efc9
Add comments explained useDeepCompareRef usage
szymonzalarski98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ import {deepEqual} from 'fast-equals'; | |
| import {useRef} from 'react'; | ||
|
|
||
| /** | ||
| * ⚠️ **WARNING: ANTI-PATTERN - AVOID IN NEW CODE** ⚠️ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! 👍
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can mark it as |
||
| * | ||
| * This hook returns a reference to the provided value, | ||
| * but only updates that reference if a deep comparison indicates that the value has changed. | ||
| * | ||
|
|
@@ -14,6 +16,22 @@ import {useRef} from 'react'; | |
| * useEffect(() => { | ||
| * // This will run not just when myArray is a new array, but also when its contents change. | ||
| * }, [deepComparedArray]); | ||
| * | ||
| * **Why this is problematic:** | ||
| * - Violates React's exhaustive deps rule (can cause stale closures) | ||
| * - Performance overhead from deep equality checks on every render | ||
| * - Incompatible with React Compiler optimizations | ||
| * - Usually indicates a data flow problem that should be fixed at the source | ||
| * | ||
| * **Use instead:** | ||
| * - `useMemo` with primitive dependencies | ||
| * - Fix selectors/data sources to return stable references | ||
| * | ||
| * **Only use when ALL of these apply:** | ||
| * - Legacy infrastructure forces new references (e.g., Onyx collections) | ||
| * - Documented why it's necessary and what the risks are | ||
| * - No feasible alternative without major refactoring | ||
| * - Performance impact measured with tests (e.g., Reassure) | ||
| */ | ||
| export default function useDeepCompareRef<T>(value: T): T | undefined { | ||
| const ref = useRef<T | undefined>(undefined); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I see that
useDeepCompareRefhook is also being used in these places:New usage (I think this is similar to
useArchivedReportsIdSet, we must useuseDeepCompareRef, but please help to review it yourself):App/src/hooks/usePrivateIsArchivedMap.ts
Line 11 in 9618ba6
You already mentioned this above:
App/src/hooks/useSidebarOrderedReports.tsx
Lines 225 to 226 in d1d85da
Please add documentation for these usages as well.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added comments to all useDeepCompareRef usages explaining why they can't easily be removed:
src/hooks/usePrivateIsArchivedMap.tsThe selector always returns a new object instance, and useMemo cannot compare by valuesrc/hooks/useSidebarOrderedReports.tsxBoth values have new references despite same content; without this, getOrderedReportIDs would be recreated on every render, causing expensive orderedReportIDs recalculation (the sortReportsToDisplayInLHN operation is a known performance bottleneck)