Skip to content

Spring isn't deeply reactive (target cannot be mutated) #14840

Open
@nasso

Description

@nasso

Describe the bug

I have something like this in my app:

export class BrokenCounter {
  #spring = new Spring({ value: 0 });

  get count() { return this.#spring.current.value }

  incr() { this.#spring.target.value++ }
  decr() { this.#spring.target.value-- }
}

Rather than re-assigning target to a new object, I directly mutate the target object.

Doing target.value++ instead of target = { value: target.value + 1 } causes the Spring to "miss" the update. It surprised me because I assumed target would be proxied by the Spring (to make it deeply reactive), just like $state. Plus, Spring already "looks deeply" into the target value (to animate individual properties and array items).

Creating a new object and assigning it to target makes for a quick workaround, but it is a bit inefficient and in some situations will lead to a lot of unnecessary garbage being created! This might be a regression from spring, which allowed mutating its target through the update method/function (I think?).

I see three potential solutions to this issue:

  1. Make Spring (and maybe others?) deeply reactive just like $state
  2. Warn against this in the documentation and hope people read it
  3. Somehow detect mutations to target and issue a warning in the console (probably just in development)

Reproduction

https://svelte.dev/playground/b9defdca3842490bb907fcfadaedfecc?version=5.16.0

Logs

No response

System Info

System:
    OS: macOS 15.1.1
    CPU: (8) arm64 Apple M2
    Memory: 72.67 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.11.0 - ~/Library/pnpm/node
    Yarn: 1.22.22 - ~/Library/pnpm/yarn
    npm: 10.9.0 - ~/Library/pnpm/npm
    pnpm: 9.14.2 - ~/Library/pnpm/pnpm
    bun: 1.1.2 - ~/.bun/bin/bun
  Browsers:
    Safari: 18.1.1

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions