TanStack / TanStack/virtual

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

Open
#1,060 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
7.1k
Forks
466
Avg merge
2d 31m
Merged PRs (30d)
13

Description

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
  • I agree to follow this project's Code of Conduct
  • 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the Virtualizer controller constructor and its hostConnected entry point, focusing on the order of addController and Virtualizer creation. Reproduce the issue by constructing the reactive controller in connectedCallback, then verify that the custom element connects without the _didMount error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.