-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Svelte 5: default values should be readonly #9763
Comments
Wanted to see if this works and... <script>
let { object = { count: 0 } } = $props();
const state = $state({ object })
</script>
<button onclick={() => {
state.object.count += 1;
}}>
state.object.count: {state.object.count}
object.count: {object.count}
</button>
|
Due to how render updates are currently batched (#9768) that is not a valid test of reactivity being triggered (in case that was the point here). <script>
let { object = { count: 0 } } = $props();
const state = $state({ object })
$effect(() => {
console.log('in state', state.object.count);
});
$effect(() => {
console.log('in object', object.count);
});
</script>
<button onclick={() => state.object.count += 1}>
+1
</button> |
I get that. I just found it interesting that in the example @Rich-Harris posted, object.count is not reactive but using it within $state then referencing the original object has the proxied version it is. I guess I thought $state would deep copy a POJO's scalar/supported values and not mutate. |
Describe the bug
As of #9739, props are readonly (unless used with
bind:
). By extension, default values should also be readonly (enforced at dev time).At present, that doesn't happen — default values can be mutated, but it's not reflected anywhere
Reproduction
https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE42P0WrDMAxFf0WYQVNWkvXVrQNjn7HsYXWczZ1jGVsZDON_n-1AKbQPRS--8tXRVWSTNiow_h6Z_ZwV4-zVObZj9OeKCL_KkMo64OJl6RyD9NpRP1gAPTv0BG_f2owweZxh03ZVtevg5jDYY3eZyGL1dn1GzjjqSauRcfKLSrtLgup5LMNARhFEwNNZSQKRnxIXSxxeIOUS8OQ8utBsb5KcFiK0gFYaLX9EbLYgeoiFudLaSoJnAftD6Uq0AY1qDX41146CTqmGqajAIV7_p7J53Xbn7I_0D3kxjdKEAQAA
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: