Skip to content

Commit 68263c8

Browse files
authored
fix: make prop fallback values deeply reactive if needed (#11804)
If a property is mutated, the assumption is that it is deeply reactive. In those cases, the fallback value should be proxified so that it also is deeply reactive. fixes #11425
1 parent ba6697d commit 68263c8

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

.changeset/strange-roses-brake.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: make prop fallback values deeply reactive if needed

packages/svelte/src/compiler/phases/3-transform/client/visitors/javascript-runes.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,14 @@ export const javascript_visitors_runes = {
246246
property.value.type === 'AssignmentPattern' ? property.value.left : property.value;
247247
assert.equal(id.type, 'Identifier');
248248
const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(id.name));
249-
const initial =
249+
let initial =
250250
binding.initial &&
251251
/** @type {import('estree').Expression} */ (visit(binding.initial));
252+
// We're adding proxy here on demand and not within the prop runtime function so that
253+
// people not using proxied state anywhere in their code don't have to pay the additional bundle size cost
254+
if (initial && binding.mutated && should_proxy_or_freeze(initial, state.scope)) {
255+
initial = b.call('$.proxy', initial);
256+
}
252257

253258
if (binding.reassigned || state.analysis.accessors || initial) {
254259
declarations.push(b.declarator(id, get_prop_source(binding, state, name, initial)));

packages/svelte/tests/runtime-runes/samples/props-default-reactivity/_config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export default test({
1717
assert.htmlEqual(
1818
target.innerHTML,
1919
`
20-
<button>mutate: 0</button>
21-
<button>reassign: 0</button>
20+
<button>mutate: 1</button>
21+
<button>reassign: 1</button>
2222
`
2323
);
2424

0 commit comments

Comments
 (0)