Skip to content

Why does Svelte5 compile code like this? #12467

Answered by brunnerh
vius asked this question in Q&A
Discussion options

You must be logged in to vote

name, being a single value, can never be reactive if not declared with $state.

data.name is a property access which makes this a bit more complicated. Svelte could potentially analyze that this particular object is not stateful, but if data were to e.g. be imported from another module, it might be stateful.

Even with analysis, it is virtually impossible to capture all cases, e.g. someone could make the object stateful later by using Object.defineProperty and making a property reference live state via a get function.

<script>
	let name = $state('tama');
	const data = { name: '...' };

	Object.defineProperty(data, 'name', {
		get() { return name; }
	});
</script>

<input bind:value={name} />
<

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@vius
Comment options

Answer selected by vius
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants