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
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)
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.
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
openedbecomes true (as it is in the {#if} block), and every timeitemgets edited,openedis set to true. This is very bug prone, and I can think of several ways how this could be slightly different, and be brokenThe example given here can be written in a more idiomatic way that sidesteps the warning completely.
REPL