sveltejs / svelte

Cybernetically enhanced web apps
https://svelte.dev
MIT License
78.04k stars 4.08k forks source link

Svelte 5: Confusing and unclear warning `state_referenced_locally` #11883

Open qupig opened 2 months ago

qupig commented 2 months ago

Describe the bug

State referenced in its own scope will never update. Did you mean to reference it inside a closure? (state_referenced_locally)

What problem was this warning intended to avoid in the first place? Why does it always appear in confusing and unclear places?

In the slightly modified official documentation example below, everything works fine and I don't understand what the warning means.

Possibly related issues: https://github.com/sveltejs/svelte/issues/9857 https://github.com/sveltejs/svelte/issues/10905

Reproduction

Svelte5-REPL

Logs

No response

System Info

svelte@5.0.0-next.148

Severity

annoyance

brunnerh commented 2 months ago

The state will go out of sync on reassignment. E.g.

let numbers = $state();
numbers = [1, 2, 3];

let obj = { a: numbers };
numbers = [4, 5, 6];

Now numbers and obj.a refer to different objects (and subsequent changes to either have no effect on the other). Mutations to objects may be captured if the reference remains the same, but assignments break this relationship. If you have a closure like a get accessor, those will always refer to the current state.

// no warning
let obj = { get a() { return numbers } };
qupig commented 2 months ago

@brunnerh Thanks, the reply explains this very well 👍. But the ⚠️ warning does not.

I don't think people can easily understand this warning, and there's no documentation or links to a more detailed explanation and examples.

paoloricciuti commented 2 months ago

@brunnerh Thanks, the reply explains this very well 👍. But the ⚠️ warning does not.

I don't think people can easily understand this warning, and there's no documentation or links to a more detailed explanation and examples.

Documentation will likely come before the actual release but I do agree that the error is too technical...however I struggle to find better frasing that is not too long.

Do you have some suggestion? I was thinking something on the line of "Using a state variable in the same scope is created can lead to out of sync...did you mean to reference it inside a closure?"

qupig commented 2 months ago

Did you mean to reference it inside a closure?

Suffice to say, seeing that phrase, I never thought it referred to using a getter.

paoloricciuti commented 2 months ago

Did you mean to reference it inside a closure?

Suffice to say, seeing that phrase, I never thought it referred to using a getter.

A getter is only a way to get it into a closure...having it in a function is fine too. I don't have ideas about how to change that specific sentence

qupig commented 2 months ago

@paoloricciuti If you don’t see the value in my feedback and are always so fond of playing word games, I believe you are capable of crafting a perfect statement as well.

paoloricciuti commented 2 months ago

@paoloricciuti If you don’t see the value in my feedback and are always so fond of playing word games, I believe you are capable of crafting a perfect statement as well.

I really don't get why you are always so oppositive... I'm telling you I agree with you, I'm asking for your help and yet you think I'm joking on you.