Skip to content
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

Closed
Rich-Harris opened this issue Dec 4, 2023 · 3 comments · Fixed by #9789
Closed

Svelte 5: default values should be readonly #9763

Rich-Harris opened this issue Dec 4, 2023 · 3 comments · Fixed by #9789
Assignees
Milestone

Comments

@Rich-Harris
Copy link
Member

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

next

Severity

annoyance

@Rich-Harris Rich-Harris self-assigned this Dec 4, 2023
@Rich-Harris Rich-Harris added this to the 5.0 milestone Dec 4, 2023
@j
Copy link

j commented Dec 4, 2023

Wanted to see if this works and...

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE41QzWrEIBB-lUEKm9Alaa9uEih9jKaHXWOorXFEJ4UivnvVQA7ZHoqX-Wa-PwxsVlp6xt8CM9dFMs5erGVnRj82A_8tNcmEPa5O5E3nhVOWhtEAqMWiI3j9UHqC2eECp6YtqNmEp8tounZXJLBx2yFZLjipWcmJcXKrjOe9QeH8r8NIWhIEwNunFAR9GgWuhjg8QUyvhwfr0PqqvuT4kQQaT-DpSjIfy1Dt-lgf-95WIjSARmglvvpQ1dAPELJV0Tabsimp8NjDcwqKcfibwCHcL2PmHliHe9duPZLt_ce9x18BDzdCxgEAAA==

<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>

@brunnerh
Copy link
Member

brunnerh commented Dec 4, 2023

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).
If you use an $effect to test for that, only the state signals:

<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>

@j
Copy link

j commented Dec 5, 2023

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). If you use an $effect to test for that, only the state signals:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants