fix(angular-virtual): wrap onChange signal write in untracked to prevent NG0600 #1295
main fromrefs/pull/1295/head·opened Fixes #1096.
onChange calls reactiveVirtualizer.set(instance) to update a linkedSignal. When a template reads virtualizer.getVirtualItems() inside a computed or effect, Angular evaluates the computed eagerly on change detection. The set() inside onChange then fires within that evaluation context, triggering NG0600: Writing to signals is not allowed in a computed or an effect.
untracked is already imported at line 11. Wrapping the set() call in untracked() escapes the reactive context and silences the error without changing behavior — the signal still updates and downstream computeds re-evaluate on the next tick.
// before
onChange: (instance, sync) => {
reactiveVirtualizer.set(instance)
...
}
// after
onChange: (instance, sync) => {
untracked(() => reactiveVirtualizer.set(instance))
...
}
Summary by CodeRabbit#
- Bug Fixes
- Virtualizer change notifications no longer track surrounding reactive reads when updating reactive state, helping prevent unintended reactive updates. DOM-flush scheduling and user-provided change callbacks continue to work as before.
- 1
piecyk reviewed
Thanks for the PR!
#1096 was reported against 4.0.1, where
getVirtualItems()→getMeasurements→notifywrote to the signal from inside a computed. Since then #1158 reworked the adapter, and on currentmainreadinggetVirtualItems()/getTotalSize()no longer triggersnotify. The remainingnotifypaths (maybeNotifyfrom the scroll/rect observers,resizeItem/measureElement,measure()) run fromafterRenderEffector event callbacks.@tanstack/angular-virtualrequires@angular/core >=20, and signal writes inside effects have been allowed since Angular 19, so those are legal.Could you share a minimal repro (StackBlitz or a failing test in
packages/angular-virtual/tests) showing NG0600 on the current version?If one exists, I'd rather fix the write-during-read at its source than mask it with
untracked. Writing toreactiveVirtualizerwhile a computed that depends on it is evaluating only settles today because the memo hits on the second pass, and it can cause extra change-detection passes (NG0100/NG0103) instead of a clear error.A changeset would also be needed, since this touches published code.
okxint(GitHub profile, opens in a new tab) linked this to fix #1096
Sign in to join this conversation.
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Advanced
Run ID:
d800bb71-d4d2-4a0b-b37c-748206bad494📥 Commits
Reviewing files that changed from the base of the PR and between 78371e851e90fd74e984deeb0c3fd8098e2cd4f3 and 562621f812d5c0692fc2be83a9b3e5eb4b5a345b.
📒 Files selected for processing (1)
packages/angular-virtual/src/index.tsIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
📝 Walkthrough
Walkthrough#
The Angular virtualizer now updates
reactiveVirtualizerinsideuntrackedwhen itsonChangecallback runs. Optional DOM-flush scheduling and the user callback remain unchanged.Changes#
Angular signal update
packages/angular-virtual/src/index.tsonChangecallback wraps thereactiveVirtualizer.set(instance)update inuntracked. The remaining callback behavior is unchanged.Priority: ➖ Normal
Estimated code review effort: 2 (Simple) | ~5 minutes
Change: Bug fix · Severity of issue fixed: Medium
Merge Risk: ⚪ Minimal · up to
56262The update avoids the reported Angular error while continuing to notify signal consumers. No actionable merge risk remains.
Architecture Summary#
Architecture risk: 🔵 Low · up to
56262The change affects 1 system.
Changed systems:
packages/angular-virtualArchitecture concerns No architecture-level concerns identified.
Review details
Systems and components
Before / after behavior
reactiveVirtualizer.set(instance)update is now wrapped inuntracked; the remainingonChangebehavior is unchanged.🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
#1096requires Angular code to prevent NG0600 whengetVirtualItems()evaluation triggersonChange. Inpackages/angular-virtual/src/index.ts,onChangenow calls `untracked(() => reactiveV…reactiveVirtualizer.set(instance)call in Angular virtualizeronChange. The change directly addresses issue#1096and does not show unrelated behavior or file…✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share