Skip to content

Commit

Permalink
fix: svelte:component evaluates props once (#8946)
Browse files Browse the repository at this point in the history
Fixes #6634
  • Loading branch information
ngtr6788 authored Jul 10, 2023
1 parent 1d05020 commit 66593c6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-ghosts-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure `svelte:component` evaluates props once
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,6 @@ export default class InlineComponentWrapper extends Wrapper {
b`if (${name}) @claim_component(${name}.$$.fragment, ${claim_nodes});`
);
}
if (updates.length) {
block.chunks.update.push(b`
${updates}
`);
}
const tmp_anchor = this.get_or_create_anchor(block, parent_node, parent_nodes);
const anchor = has_css_custom_properties ? 'null' : tmp_anchor;
const update_mount_node = has_css_custom_properties
Expand Down Expand Up @@ -481,6 +476,7 @@ export default class InlineComponentWrapper extends Wrapper {
${name} = null;
}
} else if (${switch_value}) {
${updates}
${updates.length > 0 && b`${name}.$set(${name_changes});`}
}
`);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let value;
</script>

<p>value(1) = {value}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let value;
</script>

<p>value(2) = {value}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default {
html: `
<p>value(1) = 1</p>
<button>Toggle Component</button>
`,

async test({ assert, component, window, target }) {
const button = target.querySelector('button');
await button.dispatchEvent(new window.Event('click'));
assert.htmlEqual(
target.innerHTML,
`
<p>value(2) = 2</p>
<button>Toggle Component</button>
`
);
assert.equal(component.n, 2);
await button.dispatchEvent(new window.Event('click'));
assert.htmlEqual(
target.innerHTML,
`
<p>value(1) = 3</p>
<button>Toggle Component</button>
`
);
assert.equal(component.n, 3);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import Comp1 from './Comp1.svelte';
import Comp2 from './Comp2.svelte';
export let n = 0;
let view = { comp: Comp1, fn: () => ++n };
</script>

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

<button on:click={e => view.comp = view.comp === Comp1 ? Comp2 : Comp1}>Toggle Component</button>

0 comments on commit 66593c6

Please sign in to comment.