Skip to content

Commit 66593c6

Browse files
authored
fix: svelte:component evaluates props once (#8946)
Fixes #6634
1 parent 1d05020 commit 66593c6

File tree

6 files changed

+56
-5
lines changed

6 files changed

+56
-5
lines changed

.changeset/clever-ghosts-laugh.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure `svelte:component` evaluates props once

packages/svelte/src/compiler/compile/render_dom/wrappers/InlineComponent/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,6 @@ export default class InlineComponentWrapper extends Wrapper {
436436
b`if (${name}) @claim_component(${name}.$$.fragment, ${claim_nodes});`
437437
);
438438
}
439-
if (updates.length) {
440-
block.chunks.update.push(b`
441-
${updates}
442-
`);
443-
}
444439
const tmp_anchor = this.get_or_create_anchor(block, parent_node, parent_nodes);
445440
const anchor = has_css_custom_properties ? 'null' : tmp_anchor;
446441
const update_mount_node = has_css_custom_properties
@@ -481,6 +476,7 @@ export default class InlineComponentWrapper extends Wrapper {
481476
${name} = null;
482477
}
483478
} else if (${switch_value}) {
479+
${updates}
484480
${updates.length > 0 && b`${name}.$set(${name_changes});`}
485481
}
486482
`);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let value;
3+
</script>
4+
5+
<p>value(1) = {value}</p>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let value;
3+
</script>
4+
5+
<p>value(2) = {value}</p>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export default {
2+
html: `
3+
<p>value(1) = 1</p>
4+
<button>Toggle Component</button>
5+
`,
6+
7+
async test({ assert, component, window, target }) {
8+
const button = target.querySelector('button');
9+
await button.dispatchEvent(new window.Event('click'));
10+
assert.htmlEqual(
11+
target.innerHTML,
12+
`
13+
<p>value(2) = 2</p>
14+
<button>Toggle Component</button>
15+
`
16+
);
17+
assert.equal(component.n, 2);
18+
await button.dispatchEvent(new window.Event('click'));
19+
assert.htmlEqual(
20+
target.innerHTML,
21+
`
22+
<p>value(1) = 3</p>
23+
<button>Toggle Component</button>
24+
`
25+
);
26+
assert.equal(component.n, 3);
27+
}
28+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
import Comp1 from './Comp1.svelte';
3+
import Comp2 from './Comp2.svelte';
4+
5+
export let n = 0;
6+
let view = { comp: Comp1, fn: () => ++n };
7+
</script>
8+
9+
<svelte:component this={view.comp} value={view.fn()}/>
10+
11+
<button on:click={e => view.comp = view.comp === Comp1 ? Comp2 : Comp1}>Toggle Component</button>
12+

0 commit comments

Comments
 (0)