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

<svelte:component> evaluates props twice #6634

Closed
cleiner opened this issue Aug 10, 2021 · 1 comment · Fixed by #8946
Closed

<svelte:component> evaluates props twice #6634

cleiner opened this issue Aug 10, 2021 · 1 comment · Fixed by #8946
Labels
bug compiler Changes relating to the compiler

Comments

@cleiner
Copy link

cleiner commented Aug 10, 2021

Describe the bug

In a <svelte:component> like the following, where a prop value is the result of a function call and both the function as well as the component constructor are contained in a common parent object, the function call is executed twice each time the constructor changes:

<svelte:component this={view.component} value={view.fn()}/>

Looking at the generated code, it appears that the component's p() method computes the values for switch_instance_changes without checking if it's actually required -- if the constructor (switch_value) changes, switch_instance_changes will never be used, but fn() will be called again in switch_props() instead:

function switch_props(ctx) {
	return { props: { value: /*view*/ ctx[0].fn() } };
}

p(ctx, [dirty]) {
	const switch_instance_changes = {};
	if (dirty & /*view*/ 1) switch_instance_changes.value = /*view*/ ctx[0].fn();

	if (switch_value !== (switch_value = /*view*/ ctx[0].comp)) {
		...
		switch_instance = new switch_value(switch_props(ctx));
	} else if (switch_value) {
		switch_instance.$set(switch_instance_changes);
	}
},
...

Reproduction

https://svelte.dev/repl/b00cabd142d64ab9b872ec69dbe9b0bb?version=3.42.1

Logs

No response

System Info

n/a

Severity

annoyance

@dummdidumm
Copy link
Member

Possibly the same underlying reason as for #5689

@dummdidumm dummdidumm added bug compiler Changes relating to the compiler labels Aug 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug compiler Changes relating to the compiler
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants