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

Invalid code generated when a slot prop in an #each is a function that depends on the #each context. #5565

Closed
dimfeld opened this issue Oct 22, 2020 · 1 comment · Fixed by #5607

Comments

@dimfeld
Copy link
Contributor

dimfeld commented Oct 22, 2020

Describe the bug

Code such as this:

<script>
    function setKey(key, value) { ... }
</script>

{#each keys as key}
    <slot {key} set={(value) => setKey(key, value)} />
{/each}

Generates this code:

const get_default_slot_context = ctx => ({ key: /*key*/ ctx[4], set: func });

function create_each_block(ctx) {
	let current;

	function func(...args) {
		return /*func*/ ctx[3](/*key*/ ctx[4], ...args);
	}

	const default_slot_template = /*#slots*/ ctx[2].default;
	const default_slot = create_slot(default_slot_template, ctx, /*$$scope*/ ctx[1], get_default_slot_context);

    // rest of the function here
}

func is defined inside create_each_block, not in get_default_slot_context, which causes a runtime error func is not defined.

This only happens if the value of set can't be hoisted outside the #each. If I don't use an #each loop or if I just remove the reliance on key like this set={(value) => setSomething(value)}, then everything works fine.

Looking back, 3.15 is the latest version of Svelte in which this worked.

To Reproduce

REPL: https://svelte.dev/repl/ad8e6f39cd20403dacd1be84d71e498d?version=3.29.3

Information about your Svelte project:

Svelte 3.29.3 in REPL.

Severity

Mildly inconvenient. I can work around it in this case by just using a static function for the slot prop and having the slot content just call the function with all the necessary arguments.

@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.

2 participants