svelte: Svelte 5: false warn (non_reactive_update) var is updated, but is not declared with `$state(...)`

Describe the bug

I believe the issue is related to the incomplete fix for #11269.

When variables are passed as component props, they don’t have to be reactive.

Additional issue that still exists: the warn is reported as “warn” in the svelte compiler but “error” in “eslint-plugin-svelte”, It prevents CI from passing eslint tests.

Reproduction

Svelte5-REPL

Svelte5-REPL-NEW

Logs

No response

System Info

"svelte": "5.0.0-next.143"

Severity

blocking an upgrade

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 42 (25 by maintainers)

Most upvoted comments

You do need state because the div you are binding to is inside an if…so while you are only binding to one element that element can become null and then populated again.

Here’s a minimal repro as you can see this works but also if you remove state you get the warning again but try to move the div outside the if…svelte will recognize that that can’t change and mute the warning for you since there’s no need for it anymore.

@paoloricciuti Do you think I split it into two variables for no reason? Or do you think others are not as smart as you? If I need to do something with the last opened item when I open a new item, how do you do that with one variable?

Also, does this warning tell people “you can even remove one variable”? Or do you encourage people to simply add unnecessary $state?

Store the variable currently in state in a separate variable before reassigning it? I don’t know your use case, I go for what I see in the repl you share.

We can make absurd examples all days but the reality is that a bunch of people are telling you that you are either unnecessarily complicate stuff or write code where the warning would be a good thing and yet to try to come up with more convoluted ways in which the warning could maybe make no sense.

And at the end of the day it doesn’t matter because it’s a warning and if your code really works it will work even with the warning. And if the PR in the other issue is merged you can even silence it.

Came here to write this - totally agree. The warning is correct in all the cases I saw except the one where it’s eagerly reassigned. All the others are prone to get buggy through changes to the code down the line and/or such an edge case that it doesn’t warrant the additional code complexity to detect the false positive.

You actually got lucky in your repl that it worked without $state. When html gets rendered, the current value of the non-$state variable is used. In this particular case, the html gets rendered when opened becomes true (as it is in the {#if} block), and every time item gets edited, opened is set to true. This is very bug prone, and I can think of several ways how this could be slightly different, and be broken

The example given here can be written in a more idiomatic way that sidesteps the warning completely.

  <script>
  	import Component2 from "./Component2.svelte"
  	
- 	let constVar;
  
  	async function init() {
  		try {
- 			constVar = await Promise.resolve("some promise returned value");
+ 			return await Promise.resolve("some promise returned value");
  		} catch (error) {
  			console.error(error);
+  			throw error; // if you need the catch block
  		}
  	}
  </script>
  
  {#await init()}
  	loading...
- {:then}
+ {:then constVar}
  	<Component2 {constVar} />
  {/await}

REPL