Skip to content

$state() does not update when Set changes (Set.add / Set.delete) #14013

@therealPaulPlay

Description

@therealPaulPlay

Describe the bug

Previously with Svelte 4, a let variable could be a Set and still be reactive, it was just necessary to re-assign its value. Now in Svelte 5, that does not work anymore.

Reproduction

here is a minimal example:

<script>
	// Create a reactive variable using a Set
	let setVariable = $state(new Set());

	// Interval to add to the set every 2 seconds
	let counter = 0;
	
	setInterval(() => {
		counter++;
		setVariable.add("hello" + counter);
		console.log(setVariable); // Log the set contents to see changes

		setVariable = setVariable; // Even this does not make it reactive
	}, 2000);
</script>

<h1>Hello {setVariable.size}!</h1>
<p>Check the console to see the contents of the set!</p>

The DOM does not update at all, even when I reassign the value. This worked in Svelte 4.

Logs

The logs show that the Set itself updates.

System Info

Firefox

Severity

blocking an upgrade

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