Skip to content

Infinite loop of $effect when modifying states containing arrays then reading them #16224

@GuillaumeDerval

Description

@GuillaumeDerval

Describe the bug

Modifying non-trivial $state (i.e. not a number) and then reading it inside the same $effect cause an infinite loop of calls of the effect.

Reproduction

https://svelte.dev/playground/ec99f39270c6463b9162af3432c29298?version=5.34.7

This works:

<script>
	let mystate = $state(12);
	$effect(() => {
		mystate = 13;
		if(mystate == 21)
			alert("a message");
	});
</script>
<h1>{mystate}</h1>

This doesn't:

<script>
	let mystate = $state(12);
	$effect(() => {
		mystate = [];
		if(mystate == 21)
			alert("a message");
	});
</script>
<h1>{mystate}</h1>

(the difference is that I assign [] to mystate in the $effect)

Logs

Last ten effects were:  
Array(10) [ inner(), App(), inner(), App(), inner(), App(), inner(), App(), inner(), App() ]

Uncaught Svelte error: effect_update_depth_exceeded
Maximum update depth exceeded. This can happen when a reactive block or effect repeatedly sets a new value. Svelte limits the number of nested updates to prevent infinite loops
https://svelte.dev/e/effect_update_depth_exceeded [srcdoc line 45 > eval:191:18](about:srcdoc line 45 > eval)

System Info

Tested online on the Playground with svelte 5.34.7 and Firefox 139.0.4 on macOS 15.5

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