Skip to content

Commit a2b6401

Browse files
authored
fix: svelte:component spread props change not picked up (#9006)
fix #9003, amend #8946 (comment)
1 parent 3dccf71 commit a2b6401

File tree

6 files changed

+70
-2
lines changed

6 files changed

+70
-2
lines changed

.changeset/heavy-wasps-give.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: `svelte:component` spread props change not picked up

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,21 @@ export default class InlineComponentWrapper extends Wrapper {
268268
`);
269269
if (all_dependencies.size) {
270270
const condition = renderer.dirty(Array.from(all_dependencies));
271+
if (this.node.name === 'svelte:component') {
272+
// statements will become switch_props function body
273+
// rewrite last statement, add props update logic
274+
statements[statements.length - 1] = b`
275+
if (#dirty !== undefined && ${condition}) {
276+
${props} = @get_spread_update(${levels}, [
277+
${changes}
278+
]);
279+
} else {
280+
for (let #i = 0; #i < ${levels}.length; #i += 1) {
281+
${props} = @assign(${props}, ${levels}[#i]);
282+
}
283+
}
284+
`;
285+
}
271286
updates.push(b`
272287
const ${name_changes} = ${condition} ? @get_spread_update(${levels}, [
273288
${changes}
@@ -396,7 +411,7 @@ export default class InlineComponentWrapper extends Wrapper {
396411
block.chunks.init.push(b`
397412
var ${switch_value} = ${snippet};
398413
399-
function ${switch_props}(#ctx) {
414+
function ${switch_props}(#ctx, #dirty) {
400415
${
401416
(this.node.attributes.length > 0 || this.node.bindings.length > 0) &&
402417
b`
@@ -464,7 +479,7 @@ export default class InlineComponentWrapper extends Wrapper {
464479
465480
if (${switch_value}) {
466481
${update_insert}
467-
${name} = @construct_svelte_component(${switch_value}, ${switch_props}(#ctx));
482+
${name} = @construct_svelte_component(${switch_value}, ${switch_props}(#ctx, #dirty));
468483
469484
${munged_bindings}
470485
${munged_handlers}
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>
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export default {
2+
html: `
3+
<p>value(1) = 1</p>
4+
<button>Toggle Component</button>
5+
`,
6+
7+
async test({ assert, 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+
await button.dispatchEvent(new window.Event('click'));
18+
assert.htmlEqual(
19+
target.innerHTML,
20+
`
21+
<p>value(1) = 1</p>
22+
<button>Toggle Component</button>
23+
`
24+
);
25+
}
26+
};
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+
let view = Comp1;
6+
7+
$: props = view === Comp1 ? { value: 1 } : { value: 2 };
8+
</script>
9+
10+
<svelte:component this={view} {...props} />
11+
12+
<button on:click={(e) => (view = view === Comp1 ? Comp2 : Comp1)}>Toggle Component</button>

0 commit comments

Comments
 (0)