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

Unexpected "State referenced in its own scope will never update" warning #9857

Open
geoffrich opened this issue Dec 8, 2023 · 2 comments
Open

Comments

@geoffrich
Copy link
Member

Describe the bug

When initializing the tweened store with a derived value, I get a warning: "State referenced in its own scope will never update. Did you mean to reference it inside a closure?"

Seems like a false positive to me, though I could be missing something. I want to initialize the tweened store to the initial value of the derived expression. I could duplicate the logic, but would rather avoid if possible.

Expected behavior: no warning is shown

<script>
	import { tweened } from 'svelte/motion';
	let count = $state(1);
	let doubled = $derived(count * 2);
	let tweenedDouble = tweened(doubled);

	$effect(() => {
		tweenedDouble.set(doubled);
	})

	function increment() {
		count += 1;
	}
</script>

<p>{$tweenedDouble.toFixed(2)}</p>
<button onclick={increment}>
	clicks: {count}
</button>

Reproduction

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE1WR0WrDMAxFf0WYQJNtLLSPWRIYjP3EsofWVsA0sY0tdxvG_z4nDi15lHTuvUIKbJQTOtZ8BabOM7KGvRvDXhj9maVwN5wIU-20t3zptI5baagf1EByNtoSBKAfRIUCIoxWz3DIsnrWJLU6vC3shARce0XQQeHoTFgeq_tEaH-ZkkGaCbTyhqLM8BOcHtQW87HCid3qclOvYEILHEfkVJYVdD2EpTXQTvvqkHYqilXWjl7xZWmQilucUVFy2SzyRs8dHLNkUG39uIZqTR-KfQzpT_mbFjxVsa1NotqLJ0ruWvFJ8msX7jFxvejadQ2ENWtNyIo-_WDWQo4SBWvIeozf8R-9J4iYvgEAAA==

Logs

n/a

System Info

REPL

Severity

annoyance

@Nelhoa
Copy link

Nelhoa commented May 1, 2024

The issue is still here. Very annoying

@Laurens256
Copy link

I found a (slightly hacky?) workaround for this problem. We can use untrack in this case to remove the warning but keep the behaviour the same. untrack docs

<script>
	import { tweened } from 'svelte/motion';
	import { untrack } from 'svelte';
	
	let count = $state(1);
	let doubled = $derived(count * 2);
	let tweenedDouble = tweened(untrack(() => doubled));

	$effect(() => {
		tweenedDouble.set(doubled);
	})

	function increment() {
		count += 1;
	}
</script>

<p>{$tweenedDouble.toFixed(2)}</p>
<button onclick={increment}>
	clicks: {count}
</button>

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

No branches or pull requests

3 participants