Skip to content

fix: ensure locally mutated bindable props persist with spreading props #13190

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

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fuzzy-tigers-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure locally mutated bindable props persist with spreading props
16 changes: 14 additions & 2 deletions packages/svelte/src/internal/client/reactivity/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,17 @@ export function prop(props, key, flags, fallback) {

var fallback_value = /** @type {V} */ (fallback);
var fallback_dirty = true;
var fallback_used = false;

var get_fallback = () => {
if (lazy && fallback_dirty) {
fallback_used = true;
if (fallback_dirty) {
fallback_dirty = false;
fallback_value = untrack(/** @type {() => V} */ (fallback));
if (lazy) {
fallback_value = untrack(/** @type {() => V} */ (fallback));
} else {
fallback_value = /** @type {V} */ (fallback);
}
}

return fallback_value;
Expand All @@ -264,6 +270,7 @@ export function prop(props, key, flags, fallback) {
var value = /** @type {V} */ (props[key]);
if (value === undefined) return get_fallback();
fallback_dirty = true;
fallback_used = false;
return value;
};
} else {
Expand Down Expand Up @@ -351,6 +358,11 @@ export function prop(props, key, flags, fallback) {
if (!current_value.equals(new_value)) {
from_child = true;
set(inner_current_value, new_value);
// To ensure the fallback value is consistent when used with proxies, we
// update the local fallback_value, but only if the fallback is actively used
if (fallback_used && fallback_value !== undefined) {
fallback_value = new_value;
}
get(current_value); // force a synchronisation immediately
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
accessors: false,

test({ assert, target }) {
const [btn1, btn2] = target.querySelectorAll('button');

btn1.click();
flushSync();
assert.htmlEqual(
target.innerHTML,
`<button>set color</button> <button>set options</button> bar bar`
);

btn2.click();
flushSync();
assert.htmlEqual(
target.innerHTML,
`<button>set color</button> <button>set options</button> baz bar`
);

btn1.click();
flushSync();
assert.htmlEqual(
target.innerHTML,
`<button>set color</button> <button>set options</button> foo bar`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
let { options = $bindable('foo') } = $props();

options = 'bar'
</script>

{options}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<script >
import Inner from "./inner.svelte";

let testProps = $state({
color: "red"
});
</script>

<button onclick={() => {
testProps = {
color: "blue"
};
}}>set color</button>

<button onclick={() => {
testProps = {
color: "pink",
options: 'baz'
};
}}>set options</button>

<Inner {...testProps} />

<Inner color={testProps.color} />