Skip to content

Svelte 5: array class constructor reactivity #9877

@huntabyte

Description

@huntabyte

Describe the bug

When creating a class with an array state like this:

export class ArrayRef {
		value = $state([])

		constructor(v: string[] = []) {
			this.value = v
		}
	}

The value isn't immediately reactive and requires you to push to it and then it becomes reactive (but doesn't react to the push, only follow on changes afterwards)

However, if you instead initialize it like this:

export class ArrayRef {
		value = $state([])

		constructor(v: string[] = []) {
			this.value.push(...v)
		}
	}

It works as expected

Reproduction

REPL

If you spam click the broken way you'll see it eventually updates, where the push works immediately.

Logs

No response

System Info

System:
    OS: macOS 14.2
    CPU: (12) arm64 Apple M2 Max
    Memory: 357.13 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
    pnpm: 8.10.2 - ~/Library/pnpm/pnpm
    bun: 1.0.3 - ~/.bun/bin/bun
  Browsers:
    Edge: 120.0.2210.61
    Safari: 17.2
  npmPackages:
    svelte: ^5.0.0-next.1 => 5.0.0-next.22

Severity

annoyance

Metadata

Metadata

Assignees

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