svelte
svelte copied to clipboard
state_referenced_locally warning when trying to store an initial value
Describe the bug
Not sure if i am using runes as it should be, but I get : State referenced in its own scope will never update. Did you mean to reference it inside a closure? (state_referenced_locally) when trying to store an initial value from a state
const { path } = $props()
const form = getFormContext()
const field = $derived(form.useField(path)) // access some usefull methods here depending on the path prop
const initialValue = field.value // <— warning here
const initialValue = $state.snapshot(field.value) // <— warning also
I know initialValue won't change and this is what I am expected
What am I doing wrong ?
Reproduction
https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE41UbYucMBD-K4MU1MPT9lvxVCgHhbb0hRb6Zfc-ZHXcCxcTScbrbcX_XpLo6W6P0l12yGaePDN5ZiZj0HKBJsh3YyBZh0EevOv7IAno1Ns_5hEFYZAERg26tjuFqTXvqdrLPfGuV5rgg5SoodWqgzDN3L_UHwwtyv5qJQ3BA54MlLALWZhAeLCmtqaxBq1pw7sVfkT6zmSjuk94ghKiGMrKcew-M7pPW6GUjtxSO1gUw5XzpwLlke7ju70EsHwCCXpG91DCK0OMMNpyR3F8nmd9z-QRv_kDPuzoEDSTnJ-2rskzFNkqjywOA5GSoGQteP1QjivvVPm1y6rIPLAqDjqr9rLwglpXOVo7QVYFSdCphrccmyAnPeCUPJfM4f-3aP6Ko9djsor0WvXGXUPuKbu6gut_f7wUtxoZ4Xulux9WUeAGWE0DE-IErdIdMAn4RKglE2C7LAF8yp1r7o6UjKvOVWZtO8iauJJQXxBHsVXfF9KXslH1WskRWA6hkrZ_DjmE9EuFCdR5SBrtXpOHrRp0mADmYcsf7V6bh4Y_hVN840mtBfDKDAbfcxSNLb3VaC0_AIBGGrRc2sF-DRI8MjFg5Gy8ce2pUfXOktxB6UGrc0oWTrD9NHPEa6hNuGeam9U5Lctp3pzOLnOZ6NHrFm3zW9mXvTWpRYZkS-7tSy0Cvohrg7kWKC-L6YdlRswqv2pQ80dsItcaS1yv_QbOJSfOxE8rE5T-dLpquu0gJT_IfqDouRgbMJSAKTF9RFpPT5eTK9gBhb1DuQ-45doHlYucF5nz2TF1DuDNigEl3aoc5xQmX9Zy3GZvx9lF6avbQWuU8-OUg5_2Iusr5z27cg7nJDOqVg26uR4__vj6JTWkuTzy9uTVbFQdu8stMFkYOgmcX4IGZ4EabnrBTvlBqPph7qOO6SOX16T6_I3G7ub5mXMCLMoqSdeG_8YcXqdvF9jLhy_CrMK7jP5-4O6mP2Q2eaWfBgAA
Logs
No response
System Info
System:
OS: macOS 14.4.1
CPU: (8) arm64 Apple M2
Memory: 84.00 MB / 8.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.5.1 - ~/.nvm/versions/node/v22.5.1/bin/node
npm: 10.8.2 - ~/.nvm/versions/node/v22.5.1/bin/npm
pnpm: 9.1.1 - /opt/homebrew/bin/pnpm
bun: 1.1.17 - /opt/homebrew/bin/bun
Browsers:
Brave Browser: 127.1.68.137
Chrome: 124.0.6367.208
Safari: 17.4.1
npmPackages:
svelte: 5.0.0-next.222 => 5.0.0-next.222
Severity
annoyance
Then you can just ignore the warning :)
Then you can just ignore the warning :)
Well this is what I am doing actually... I think having a warning on a correct implementation is kind of weird, from a developper point of view it makes you feel like you are doing something the bad way.
It's unclear especially here :
const initialValue = $state.snapshot(field.value)
It's obvious that I don't want any update as it is a snapshot...
Maybe I missed something... is there a better way to break reactivity on purpose ? I mean without warning...
If it won't change, surely it makes little sense to make it $state or $derived?
The warning is that you're doing something a little 'odd' that may not be what you intended, by giving mixed signals (pun intended) that the value is both a constant and reactive.
Then you can just ignore the warning :)
Well this is what I am doing actually... I think having a warning on a correct implementation is kind of weird, from a developper point of view it makes you feel like you are doing something the bad way.
It's a warning and not an error exactly because we can't know if your implementation is correct. We warn you so that if you are sure it's fine you can ignore that warning.
It's unclear especially here :
const initialValue = $state.snapshot(field.value)It's obvious that I don't want any update as it is a snapshot...
If I do something like this
$effect(()=>{
console.log($state.snapshot(field.value));
});
I will actually log every time field.value changes so it's not much different from the warn you'll get if you write.
let initialValue = field.value;
Maybe I missed something... is there a better way to break reactivity on purpose ? I mean without warning...
You can do all sort of things but...why? Just ignore the warning leaving a comment for your future self.
You can use an iife to make the warning go away, like so:
let initialValue = (() => $state.snapshot(field.value))()
You can do all sort of things but...why? Just ignore the warning leaving a comment for your future self.
@paoloricciuti Is there a way to suppress such a warning? Like some // svelte-disable-next-line comment? I understand that this is a warning precisely because it has false positives, but I (and I suppose others in this thread) like to keep my build process clean of any warnings so that I know when something changes in the future just by seeing the (new) warning - if I have a wall of warnings every time I build, this breaks such an assumption for me...
You can do all sort of things but...why? Just ignore the warning leaving a comment for your future self.
@paoloricciuti Is there a way to suppress such a warning? Like some
// svelte-disable-next-linecomment? I understand that this is a warning precisely because it has false positives, but I (and I suppose others in this thread) like to keep my build process clean of any warnings so that I know when something changes in the future just by seeing the (new) warning - if I have a wall of warnings every time I build, this breaks such an assumption for me...
Yeah that's what I meant by ignore the warning:
// svelte-ignore state_referenced_locally
const initialValue = field.value
Btw let me also close this issue
If you, like me, got to the bottom of the thread to learn that these are not equal:
/** svelte-ignore state_referenced_locally */
// svelte-ignore state_referenced_locally
Just know someone out there feels your pain.