-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
The data
prop and subsequent $derived
not mutable by default
#13626
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
Comments
IMO state would be a good default but switching to raw when needed should be as simple as possible, although this may mean making |
I think this is intentional (though Rich might hop in here and tell me I'm wrong). What you can do (and what we prefer) is to make it explicit that you intend to optimistically update something: const { data } = $props();
const likesCheeseburgers = $derived(data.likesCheeseburgers);
likesCheeseburgers = true; |
Right, but that only works if you reassign |
Exactly! The examples given by the $derived / data example are just too simplified. Usually data contains a complex structure, like for example some business data, e.g., a customer that you return as |
Most of the time - and the way the APIs work push you in that direction - it's better to not mutate, instead reassign the data. In other words, instead of |
Then what's the point of having the rune system in the first place then? |
Universal reactivity, easier to read code, more predictable behavior, being able to deprecate (and at some point remove) APIs like |
It's easy to say when data.deeply.nested.object.property= 'new value' vs this: data = {...data, deeply: {...data.deeply, nested: {...data.deeply.nested, object: {...data.deeply.nested.object, property: 'new value'}}}}; It wasn't like this in Svelte 4. I feel like this is a purist ideology similar to ' |
I also think this might be worth considering but if this is enabled by adding a state proxy to |
I can't think of anything this change would 'break', but perhaps I'm ignorant. I think this is more an enhancement rather than a breaking change. Like moving from read-only |
It currently doesn't affect I presume wrapping |
If you're talking about let localData = $derived($state.snapshot(data)); If a dev wants to mutate |
The change to Adding a proxy is a breaking change because certain APIs just do not work with them (e.g. |
So it seems the least invasive solution is proxifying let localData= $derived($state(data)); It's simple and straight-forward for anyone new to Svelte(Kit) to understand |
Describe the bug
Now that
$derived
has been made writable. This is intended to make optimistic UI updates possible without having to use the$state
in$derived.by
dance. However, it still doesn't entirely solve the problem becausedata
is a$state.raw
, which means mutations todata
or its deriveds won't update the UI. To make it work like Svelte 4 did, the trick is still needed, which is kinda ugly and not beginner friendly.I believe making
data
a full$state
instead of$state.raw
would solve this problem. Any reasons not to do so?Reproduction
https://svelte.dev/playground/a69b38c2e041412989ca3edb509a0138?version=5.25.2
Logs
System Info
Severity
annoyance
Additional Information
No response
The text was updated successfully, but these errors were encountered: