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

state_referenced_locally warning when trying to store an initial value #12877

Open
bienoubien-studio opened this issue Aug 16, 2024 · 4 comments

Comments

@bienoubien-studio
Copy link

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

@paoloricciuti
Copy link
Member

Then you can just ignore the warning :)

@bienoubien-studio
Copy link
Author

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...

@CaptainCodeman
Copy link

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.

@paoloricciuti
Copy link
Member

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.

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