virtual icon indicating copy to clipboard operation
virtual copied to clipboard

Lit: `hostConnected` can fire *before* the `Virtualizer` instance has been created

Open jpzwarte opened this issue 3 months ago • 1 comments

Describe the bug

  constructor(
    host: ReactiveControllerHost,
    options: VirtualizerOptions<TScrollElement, TItemElement>,
  ) {
    ;(this.host = host).addController(this)

    const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {
      ...options,
      onChange: (instance, sync) => {
        this.host.updateComplete.then(() => this.host.requestUpdate())
        options.onChange?.(instance, sync)
      },
    }
    this.virtualizer = new Virtualizer(resolvedOptions)
  }

When the constructor is called for an already connected custom element, calling addController will cause hostConnected to be called. This then fails because this.virtualizer is still undefined at that point.

Cannot read properties of undefined (reading '_didMount')

  async hostConnected() {
    this.cleanup = this.virtualizer._didMount()
  }

Your minimal, reproducible example

Steps to reproduce

Move construction of the reactive controller in an example to connectedCallback.

Expected behavior

Not throw an error

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

tanstack-virtual version

latest

TypeScript version

No response

Additional context

No response

Terms & Code of Conduct

  • [x] I agree to follow this project's Code of Conduct
  • [x] I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

jpzwarte avatar Oct 01 '25 12:10 jpzwarte

Fixed in #1061

jpzwarte avatar Oct 01 '25 12:10 jpzwarte