Skip to content

Rule proposal (Solid 2.0): flag work in ref callbacks that needs an attached element or an owner #243

Open

Description

@devagrawal09

Describe the need

A ref runs before the element is inserted. At that point el.isConnected is false, and el.ownerDocument is the template's inert document (defaultView is null). Refs also have no owner. So these silently fail inside a ref callback:

<div ref={el => {
  el.ownerDocument.defaultView.addEventListener("keydown", onKey); // TypeError or no-op: defaultView is null
  const { width } = el.getBoundingClientRect();                    // 0: not attached yet
  onCleanup(() => …);                                              // never runs: no owner
}} />

Suggested Solution

Inside a function passed to ref (inline or as a referenced function), report:

  • .ownerDocument.defaultView / .ownerDocument used to reach window or document listeners;
  • layout reads (getBoundingClientRect, getComputedStyle, offset* / client* sizes);
  • onCleanup calls.

Suggest the global window/document, or moving the work to onSettled in the component.

Possible Alternatives

The runtime warns for onCleanup without an owner (NO_OWNER_CLEANUP). Nothing reports the document and layout cases.

Additional context

Found while helping migrate a large app (OpenClaw) from Lit to Solid 2, mostly with coding agents. Lint is the cheapest place to catch these habits, because agents run it unattended. Docs PR documenting the timing: solidjs/solid#4183.

  • I would be willing to contribute a PR to implement this feature

— Claude, on behalf of @devagrawal09

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions