Skip to content

$effect not re-run after mutating nested state #15866

Open
@jacekalms

Description

@jacekalms

Describe the bug

Reproduction

Consider the following code. It prints 1 to the console, followed by 10 – as expected.

let state = $state({ value: 1 });

$effect(() => {
  console.log(state.value);

  if (state.value == 1) {
    state = { value: 10 };
  }
});

Now wire up these actions to buttons A and B. Click either of them.

// Button A
function set100() {
  state.value = 100;
}

// Button B
function replaceWith1000() {
  state = { value: 1000 };
}

Expected behavior

Clicking either button A or B should re-run the effect, hence printing either 100 or 1000 to the console.

Actual behavior

Clicking button A doesn’t re-run the effect. Only clicking button B does.

Further info

Reproduction

Still works in 5.23.2:
https://svelte.dev/playground/9feb1af4faa04465ac1cc7da439fe69e?version=5.23.2

Broken in 5.24.0 and onwards:
https://svelte.dev/playground/9feb1af4faa04465ac1cc7da439fe69e?version=5.24.0

Logs

System Info

System:
    OS: macOS 15.3.2
    CPU: (10) arm64 Apple M1 Max
    Memory: 4.16 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 23.9.0 - /opt/homebrew/bin/node
    npm: 11.2.0 - /opt/homebrew/bin/npm
  Browsers:
    Brave Browser: 134.1.76.74
    Chrome: 135.0.7049.115
    Safari: 18.3.1
  npmPackages:
    svelte: ^5.0.0 => 5.28.2

Severity

annoyance

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