sveltejs / svelte

Cybernetically enhanced web apps
https://svelte.dev
MIT License
76.8k stars 3.98k forks source link

Svelte 5: $state object mutation affects parent #11443

Closed max-got closed 2 weeks ago

max-got commented 2 weeks ago

Describe the bug

Details I hope this isn't intended. Modifying the newObj $state object unexpectedly affects the initialObj $state object as well, see the following: ```svelte initialObj.count : {initialObj.count} newObj.count : {newObj.count} ```

nevermind...

need to adapt to svelte 5... see Svelte 4 REPL

Reproduction

Repl

Logs

No response

System Info

svelte: next (svelte@5.0.0-next.121)

Severity

blocking an upgrade

PuruVJ commented 2 weeks ago

This is intentional I believe, just how JS works, you should use $state(structuredClone($unstate(initialObject)))

7nik commented 2 weeks ago

In your Svelte 4 REPL, you mutate $initalObj's value without triggering the store, so it isn't re-rendered. More correct Svelte 4 example.

In Svelte 5 $state objects are just proxied (correct me if I'm wrong) to the original. Example.