Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bind:this to each value compiles into undefined variable names #4517

Closed
pushkine opened this issue Mar 6, 2020 · 5 comments · Fixed by #4861
Closed

bind:this to each value compiles into undefined variable names #4517

pushkine opened this issue Mar 6, 2020 · 5 comments · Fixed by #4861

Comments

@pushkine
Copy link
Contributor

pushkine commented Mar 6, 2020

<script>
    const refs = [1,2,3]
</script>

{#each refs as ref}
    <div bind:this={ref}/>
{/each}

ERROR: each_value is not defined

https://svelte.dev/repl/2d4004d1ee40459a9022c97204529bb6?version=3.19.2

in fact neither each_value or ref_index are defined in the compiled code

function instance($$self, $$props, $$invalidate) {
	const refs = [1, 2, 3];

	function div_binding($$value, ref) {
		if (each_value[ref_index] === $$value) return;

		binding_callbacks[$$value ? "unshift" : "push"](() => {
			each_value[ref_index] = $$value;
		});
	}

	return [refs, div_binding];
}
@Come2Daddy
Copy link

What is the purpose of this exactly ? Can't see a use case for this.
Thx

@pushkine
Copy link
Contributor Author

pushkine commented Mar 6, 2020

@Come2Daddy none, I just bumped into this bug while trying to simplify #4512

@antony
Copy link
Member

antony commented Mar 6, 2020

You're trying to write to ref which should probably be immutable, since it is declared by your loop.

Whether this is something useful to enforce, I don't know.

@pushkine
Copy link
Contributor Author

pushkine commented Mar 6, 2020

You're trying to write to ref which should probably be immutable, since it is declared by your loop.

@antony you might have a proposal to make there but that's not how svelte works at the moment

probably every binding but bind:this works as you'd expect
https://svelte.dev/repl/3c7139dce64346e5af207f7123ff507c?version=3.19.2

@Conduitry
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants