-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure props passed to components via mount are updateable (#14210)
The detection whether or not props are writable is buggy; it doesn't take into account the case when instantiating components via `mount` or legacy-`new` Fixes #14161 This posed an interesting question: What to do about (non-)`bind:`able properties? The answer I arrived on was: Treat it as if everything that _can_ be bound _is_ treated as bound, and everything else as readonly. In other words, if you're reassigning a prop, it will diverge from the passed in props if it's not bindable or not set in the parent, otherwise it will mutate the passed in props. I think that makes the most sense, given that you can't control this behavior from the outside.
- Loading branch information
1 parent
7fd3774
commit e379319
Showing
8 changed files
with
124 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
fix: ensure props passed to components via mount are updateable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/svelte/tests/runtime-runes/samples/mount-props-updates/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { flushSync } from 'svelte'; | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
test({ assert, target }) { | ||
assert.htmlEqual( | ||
target.innerHTML, | ||
// The buz fallback does not propagate back up | ||
` | ||
<button>reset</button> foo baz | ||
<div><button>update</button> foo bar baz buz</div> | ||
<div><button>update</button> foo bar baz buz</div> | ||
` | ||
); | ||
|
||
const [btn1, btn2, btn3] = target.querySelectorAll('button'); | ||
|
||
btn2.click(); | ||
btn3.click(); | ||
flushSync(); | ||
assert.htmlEqual( | ||
target.innerHTML, | ||
// bar is not set in the parent because it's a readonly property | ||
// baz is not set in the parent because while it's a bindable property, | ||
// it wasn't set initially so it's treated as a readonly proeprty | ||
` | ||
<button>reset</button> foo 3 | ||
<div><button>update</button> 1 2 3 4</div> | ||
<div><button>update</button> 1 2 3 4</div> | ||
` | ||
); | ||
|
||
btn1.click(); | ||
flushSync(); | ||
assert.htmlEqual( | ||
target.innerHTML, | ||
// Because foo is a readonly property, component.svelte diverges locally from it, | ||
// and the passed in property keeps the initial value of foo. This is why it stays | ||
// at 1, because foo is not updated to a different value. | ||
` | ||
<button>reset</button> foo bar baz buz | ||
<div><button>update</button> 1 bar baz buz</div> | ||
<div><button>update</button> 1 bar baz buz</div> | ||
` | ||
); | ||
} | ||
}); |
17 changes: 17 additions & 0 deletions
17
packages/svelte/tests/runtime-runes/samples/mount-props-updates/component.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<script> | ||
let { foo, bar = 'bar', baz = $bindable(), buz = $bindable('buz') } = $props(); | ||
</script> | ||
|
||
<button | ||
onclick={() => { | ||
foo = '1'; | ||
bar = '2'; | ||
baz = '3'; | ||
buz = '4'; | ||
}}>update</button | ||
> | ||
|
||
{foo} | ||
{bar} | ||
{baz} | ||
{buz} |
30 changes: 30 additions & 0 deletions
30
packages/svelte/tests/runtime-runes/samples/mount-props-updates/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<script> | ||
import { createClassComponent } from 'svelte/legacy'; | ||
import Component from './component.svelte'; | ||
import { mount, onMount } from 'svelte'; | ||
let div1, div2; | ||
let legacy; | ||
const props = $state({ foo: 'foo', baz: 'baz' }); | ||
onMount(() => { | ||
legacy = createClassComponent({ | ||
component: Component, | ||
target: div1, | ||
props: { foo: 'foo', baz: 'baz' } | ||
}); | ||
mount(Component, { target: div2, props }); | ||
}); | ||
</script> | ||
|
||
<button | ||
onclick={() => { | ||
legacy.$set({ foo: 'foo', bar: 'bar', baz: 'baz', buz: 'buz' }); | ||
props.foo = 'foo'; | ||
props.bar = 'bar'; | ||
props.baz = 'baz'; | ||
props.buz = 'buz'; | ||
}}>reset</button | ||
> {props.foo} {props.bar} {props.baz} {props.buz} | ||
<div bind:this={div1}></div> | ||
<div bind:this={div2}></div> |