Skip to content

[Svelte5] Very strange behavior with a $state() created by a $state.snapshot() #13123

Closed
@fsoft72

Description

@fsoft72

Describe the bug

It seems that $state()s created by copying a $state.snapshot() behave differently.
I have created a small example that does the following:

  1. Create a small $state() called user
  2. Pass the user to a component
  3. The component creates a new $state() by cloning data in this way: $state($state.snapshot(_user))
  4. The new user object is passed to some <input> types with the bind:value directive
  5. When the save button is pressed, the old values are returned to the onsave callback ... unless you spread the object.

So, in short:

  • onsave(user) will return the OLD values, even if they are saved inside the new user object
  • onsave({...user}) will return the correct values

Tested against: svelte 5.0.0-next.243

Reproduction

I have created a small REPL Here

Logs

No response

System Info

System:
    OS: Linux 6.8 Ubuntu 24.04 LTS 24.04 LTS (Noble Numbat)
    CPU: (24) x64 AMD Ryzen 9 7900X 12-Core Processor
    Memory: 53.73 GB / 62.01 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 20.11.1 - ~/.nvm/versions/node/v20.11.1/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.1/bin/npm
    pnpm: 9.9.0 - ~/.local/share/pnpm/pnpm
    bun: 1.0.0 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 127.1.68.137
    Chrome: 127.0.6533.88


### Severity

blocking all usage of svelte

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions