Skip to content

Please add/document bind:this setter support #16547

@timephy

Description

@timephy

Describe the problem

I am doing something like this:

{#each Object.entries(tabs) as [tabId, tab] (tabId)}
    <TabView selected={tabId === selected} bind:this={tabViews[tabId]}>
        Content
    </TabView>
{/each}

Binding directly to an object const tabViews = {} via bind:this={tabViews[tabId]} does not work when adding more tabs dynamically, then references to existing ("cached" by #each because of given key (tabId)) tabs are lost. See this result after adding more tabs: {player: null, group: Proxy(Object), call: Proxy(Object), chat: Proxy(Object)}. ❗

I then tried binding to a map using getters and setters as described in https://svelte.dev/docs/svelte/bind#Function-bindings like so bind:this={null, (v) => tabViews.set(tabId, v)}. This failed with a runtime error: Binding as described in https://svelte.dev/docs/svelte/bind#Function-bindings

Therefore I changed the code to this bind:this={() => null, (v) => tabViews.set(tabId, v)} which finally worked ✅

Describe the proposed solution

Request

Please add support for the mentioned https://svelte.dev/docs/svelte/bind#Function-bindings for bind:this.

Or if that is not sensible, then maybe add this behaviour to the docs?

Importance

nice to have

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