Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Svelte 5: array class constructor reactivity #9877

Closed
huntabyte opened this issue Dec 9, 2023 · 0 comments · Fixed by #9888
Closed

Svelte 5: array class constructor reactivity #9877

huntabyte opened this issue Dec 9, 2023 · 0 comments · Fixed by #9888
Assignees

Comments

@huntabyte
Copy link
Member

huntabyte commented Dec 9, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants