-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
list
is not reactive, but list.length
is
#12439
Comments
You can do Also a bit annoyed that the docs example isn't a real example that you can paste into the REPL and watch it actually do something. |
Definitely don't want to clone the entire state every time it changes. |
This is only true if you push to list instead of reassigning, it works if you reassign. That's fine grained at work: if you push to an array only the length will, and probably should, be invalidated.
It really depends on the use case: snapshot will read every element of the array and add a dependency to it so if you want to react to every change in the array (length, reassign of array, change of an element) then it's the way to go. I'm this case he just want to re-enroll when the length changes so you can read length.
You are right but that's just because there's no change to |
Because
Yea, that example should be fixed. |
Given that it should be encouraged to mutate state like this instead of reassigning it (for performance reasons), I think there should be a warning similar to const items = $state([]);
$effect(() => {
items;
// ^ warn: Referencing the root of deeply reactive state will not trigger changes when the state is mutated.
}); Alternatively, a import { track } from 'svelte';
const huge_form = $state({ /* lots of deep nested data */ });
let has_changed = $state(false);
$effect(() => {
track(huge_form);
// or track.deep(huge_form)?
has_changed = true;
}); |
I think directly referencing dependency should be discouraged enough and it's only really needed in rare cases. And given that this is just how fine grained reactivity works I feel like a warning would be kinda wrong (but that's just a gut feeling)
You can't have a |
I disagree because the
This is wrong as of yesterday, see #12413 |
You can use function readAll(v) {
if (typeof v === 'object' && v !== null)
for (let k in v) readAll(v[k]);
}
$effect(() => {
readAll(items);
...
}) which should be even faster. Not sure if this needs to be provided by svelte. But I'm not against it either. |
Is the new example supposed to autoscroll? I added a button to add messages and it seems like nothing happens - REPL I think I've used |
The example leaves out a style for the div for brevity - maybe we should add it to make that clearer? |
Yes please. If it takes up too much space on the page maybe something like |
Describe the bug
The example in the
$effect.pre
docs describes referencing a list to trigger an$effect
. However, you need to referencelist.length
instead.Reproduction
REPL
Logs
No response
System Info
Severity
blocking an upgrade
The text was updated successfully, but these errors were encountered: