Skip to content
Source
Sign inSign up
TanStack/virtual

fix(angular-virtual): RuntimeError: NG0600: Writing to signals is not allowed in a computed or an effect by default #1109

ClosedStallion8472(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/1109/headopened

🎯 Changes

Fixes #1096

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).
    1. 🦋 Changeset detected

      Latest commit: fae1553f2ef5217a0ddcde71977abc30448ba464

      The changes in this PR will be included in the next version bump.

      This PR includes changesets to release 1 package
      NameType
      @tanstack/angular-virtualPatch

      Not sure what this means? Click here to learn what changesets are.

      Click here if you're a maintainer who wants to add another changeset to this PR

    2. Any timeline for the release of this fix ?

    3. I’ll take a look at this soon, thanks!

    4. Have you got more informations on when this pull request can be merged @piecyk ? 🙏

    5. Pull request overview

      This PR addresses Angular runtime error NG0600 by changing how @tanstack/angular-virtual exposes reactive virtualizer state, aiming to avoid signal writes occurring during computed/effect evaluation.

      Changes:

      • Removes the proxyVirtualizer-based implementation that created per-property computed signals on-demand.
      • Introduces a new Proxy wrapper around a single Virtualizer instance and exposes selected reactive properties via dedicated Angular signals updated from onChange.
      • Adds a changeset to release the fix as a patch for @tanstack/angular-virtual.

      Reviewed changes

      Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

      FileDescription
      packages/angular-virtual/src/proxy.tsRemoves the previous proxy-based lazy-init + computed-signal wrapping implementation.
      packages/angular-virtual/src/index.tsReworks the adapter to use a concrete Virtualizer instance plus explicit Angular signals updated in onChange.
      .changeset/clean-ends-wish.mdDeclares a patch release for the Angular adapter fix.

      💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

      packages/angular-virtual/src/index.ts lines 56 to 57
      @@ -36,0 +37,21 @@
      37+ const isScrolling = signal(instance.isScrolling)
      38+ const range = signal(instance.range, { equal: () => false })
      39+ const scrollDirection = signal(instance.scrollDirection)
      40+ const scrollElement = signal(instance.scrollElement)
      41+ const scrollOffset = signal(instance.scrollOffset)
      42+ const scrollRect = signal(instance.scrollRect)
      43+
      44+ const handler = {
      45+ get(
      46+ target: Virtualizer<TScrollElement, TItemElement>,
      47+ prop: keyof Virtualizer<TScrollElement, TItemElement>,
      48+ ) {
      49+ switch (prop) {
      50+ case 'getVirtualItems':
      51+ return virtualItems
      52+ case 'getTotalSize':
      53+ return totalSize
      54+ case 'isScrolling':
      55+ return isScrolling
      56+ case 'options':
      57+ return options

      options property on the returned AngularVirtualizer proxy is currently mapped to the input options signal rather than instance.options (the fully-normalized options object that Virtualizer actually uses). This changes runtime semantics and also diverges from AngularVirtualizer['options'] (which is typed as Signal<Virtualizer['options']>). Consider exposing a dedicated options signal that is updated from instance.options (e.g. in onChange and/or after setOptions) instead of returning the adapter’s options input signal here.

      packages/angular-virtual/src/index.ts line 95
      @@ -43,12 +80,16 @@
      4380 () => {
      4481 const _options = options()
      45- lazyInit()
      46- virtualizerSignal.set(virtualizer)
      47- virtualizer.setOptions({
      82+ instance.setOptions({
      4883 ..._options,
      4984 onChange: (instance, sync) => {
      50- // update virtualizerSignal so that dependent computeds recompute.
      51- virtualizerSignal.set(instance)
      85+ virtualItems.set(instance.getVirtualItems())
      86+ totalSize.set(instance.getTotalSize())
      87+ isScrolling.set(instance.isScrolling)
      88+ range.set(instance.range)
      89+ scrollDirection.set(instance.scrollDirection)
      90+ scrollElement.set(instance.scrollElement)
      91+ scrollOffset.set(instance.scrollOffset)
      92+ scrollRect.set(instance.scrollRect)
      5293 _options.onChange?.(instance, sync)
      5394 },
      5495 })

      The adapter signals (virtualItems, totalSize, range, etc.) are only updated inside the onChange callback. However, Virtualizer.setOptions() does not itself trigger notify(), and _willUpdate() only notifies when the scroll element changes (or via subsequent scroll/resize observers). This means option-only changes (e.g. count changes from a signal, which is a documented use case) can leave these signals stale until an unrelated scroll/resize happens. To keep the Angular signals consistent, update them once after applying options (or otherwise force a notify) within this effect run.

            })
            // Ensure adapter signals stay in sync even when only options change
            virtualItems.set(instance.getVirtualItems())
            totalSize.set(instance.getTotalSize())
            isScrolling.set(instance.isScrolling)
            range.set(instance.range)
            scrollDirection.set(instance.scrollDirection)
            scrollElement.set(instance.scrollElement)
            scrollOffset.set(instance.scrollOffset)
            scrollRect.set(instance.scrollRect)
      packages/angular-virtual/src/index.ts line 67
      @@ -36,0 +47,21 @@
      47+ prop: keyof Virtualizer<TScrollElement, TItemElement>,
      48+ ) {
      49+ switch (prop) {
      50+ case 'getVirtualItems':
      51+ return virtualItems
      52+ case 'getTotalSize':
      53+ return totalSize
      54+ case 'isScrolling':
      55+ return isScrolling
      56+ case 'options':
      57+ return options
      58+ case 'range':
      59+ return range
      60+ case 'scrollDirection':
      61+ return scrollDirection
      62+ case 'scrollElement':
      63+ return scrollElement
      64+ case 'scrollOffset':
      65+ return scrollOffset
      66+ case 'scrollRect':
      67+ return scrollRect

      Previously, the adapter wrapped several argument-taking Virtualizer methods (e.g. getOffsetForIndex, getVirtualItemForOffset, indexFromElement) so their results became reactive to virtualizer updates when used from signal-driven templates/computeds. With the new proxy, these methods fall through to Reflect.get and no longer read any Angular signal, so their return values won’t automatically update when the virtualizer state changes. If this reactivity was intentional (it was explicitly implemented in the removed proxy.ts), consider reintroducing a reactive wrapper for these methods or documenting the behavior change as it can break existing consumers.

                return scrollRect
              case 'getOffsetForIndex':
              case 'getVirtualItemForOffset':
              case 'indexFromElement':
                // Wrap argument-taking query methods so they read a signal and stay reactive
                return (...args: unknown[]) => {
                  // Access a signal that updates on virtualizer changes to register dependency
                  virtualItems()
                  // Delegate to the underlying Virtualizer method
                  // eslint-disable-next-line @typescript-eslint/no-explicit-any
                  return (target as any)[prop](...args)
                }
    6. Hello 👋 I'm working on improving angular support from some of the libraries on the TanStack ecosystem. The Query adapter I'm working on has similar requirements and I can continue working in this issue if needed.

      packages/angular-virtual/src/index.ts line 31
      @@ -30,7 +28,4 @@
      3028 >(
      3129 options: Signal<VirtualizerOptions<TScrollElement, TItemElement>>,
      3230 ): AngularVirtualizer<TScrollElement, TItemElement> {
      33- let virtualizer: Virtualizer<TScrollElement, TItemElement>
      34- function lazyInit() {
      35- virtualizer ??= new Virtualizer(options())
      36- return virtualizer
      31+ const instance = new Virtualizer<TScrollElement, TItemElement>(options())

      You can't just call options() directly, since if that signal depends on Input signals, it will break with NG0952/NG0950. I wrote about it here for the Store adapter.

      packages/angular-virtual/src/index.ts line 92
      @@ -40,12 +78,15 @@
      4078
      41- // two-way sync options
      4279 effect(
      4380 () => {
      4481 const _options = options()
      45- lazyInit()
      46- virtualizerSignal.set(virtualizer)
      47- virtualizer.setOptions({
      82+ instance.setOptions({
      4883 ..._options,
      4984 onChange: (instance, sync) => {
      50- // update virtualizerSignal so that dependent computeds recompute.
      51- virtualizerSignal.set(instance)
      85+ virtualItems.set(instance.getVirtualItems())
      86+ totalSize.set(instance.getTotalSize())
      87+ isScrolling.set(instance.isScrolling)
      88+ range.set(instance.range)
      89+ scrollDirection.set(instance.scrollDirection)
      90+ scrollElement.set(instance.scrollElement)
      91+ scrollOffset.set(instance.scrollOffset)
      92+ scrollRect.set(instance.scrollRect)

      I think you can keep the signal proxy pattern BUT if it works by lazy initializing the signal with a computed and by declaring what is a function vs a signal.

      The Angular upgrade has a similar pattern here and here.

    7. Hi @Stallion8472 Thanks again for opening this PR, really appreciate it!

      Since @benjavicente mentioned they’re working on improving Angular support across the TanStack ecosystem, I was wondering if you’d be comfortable with them picking this up and continuing the work here?

    8. @piecyk Yeah I have no issue with others picking this up and continuing the work

    9. @piecyk Yeah I have no issue with others picking this up and continuing the work

      Thanks @Stallion8472, @benjavicente feel free to pick this up 👍

    10. piecykcommented

      Again @Stallion8472 thanks for the PR. I’m going to close this in favor of #1158, which reworks the Angular adapter around Angular 19 signal primitives and is intended to address the same NG0600/reactivity issues in a broader way. If it turns out there’s still a repro left after #1158 lands, we can reopen or follow up with a narrower fix.

    11. piecyk closed this

Sign in to join this conversation.