Skip to content

Function changing state in class does not trigger reactivity #15098

@ecstrema

Description

@ecstrema

Describe the bug

I can't seem to wrap my head around svelte's new reactivity system.

Below is a simple example, and I don't understand why it is not working. You just create a state class:

class Test {
  ready = $state(false);

	init() {
		this.ready = true
	}
}

export const test = new Test();

and import it in the other component.

<script>
	import {test} from "./test.svelte"
</script>

<h1>{test.ready}</h1>

<button onclick={test.init}>Click this to set the above to true</button>

But the UI doesn't update.

Reproduction

https://svelte.dev/playground/0d52f8525fbf4e4382eda285c426aec7?version=5.19.2

Logs

System Info

REPL

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