Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/hooks/useTodoCounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,12 @@ function useTodoCounts(enabled = true): {counts: TodoCounts; singleReportIDs: To
const hasChanged = !frozen || TODO_KEYS.some((key) => frozen.counts[key] !== counts[key] || frozen.singleReportIDs[key] !== singleReportIDs[key]);
if (hasChanged) {
setFrozen(value);
return value;
}

return value;
// Nothing changed — return the previously stored object so consumers keep a stable reference and
// memoized children don't re-render on unrelated Onyx writes to the subscribed collections.
return frozen;
}

export default useTodoCounts;
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/useTodoCountsTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,31 @@ describe('useTodoCounts', () => {
expect(result.current.singleReportIDs[CONST.SEARCH.SEARCH_KEYS.PAY]).toBeUndefined();
});

it('keeps a stable result reference when an Onyx write does not change the counts', async () => {
const {result} = await renderTodoCounts();
const firstResult = result.current;

// Rename an excluded chat report - the REPORT collection subscription fires, but no bucket changes.
await act(async () => {
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${EXCLUDED_REPORT_IDS.at(0)}`, {reportName: 'Renamed chat'});
await waitForBatchedUpdates();
});

expect(result.current).toBe(firstResult);

// A write that changes a count must produce a new reference.
await act(async () => {
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${SUBMIT_REPORT_IDS.at(0)}`, {
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
});
await waitForBatchedUpdates();
});

expect(result.current).not.toBe(firstResult);
expect(result.current.counts[CONST.SEARCH.SEARCH_KEYS.SUBMIT]).toBe(3);
});

it('updates the submit count when a report state changes', async () => {
const {result} = await renderTodoCounts();
expect(result.current.counts[CONST.SEARCH.SEARCH_KEYS.SUBMIT]).toBe(4);
Expand Down
Loading