-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Object ownership not added for $state
inside $derived
#15164
Comments
- `$derived` can contain `$state` declarations so we cannot ignore them, so this reverts #14533 - instead, we add equality checks to not do this expensive work unnecessarily - this also adds a render effect similar to the class ownership addition when it detects a getter on a POJO during ownership addition fixes #15164
- `$derived` can contain `$state` declarations so we cannot ignore them, so this reverts #14533 - instead, we add equality checks to not do this expensive work unnecessarily - this also adds a render effect similar to the class ownership addition when it detects a getter on a POJO during ownership addition fixes #15164
one definite drawback of this linked implementation via a derived is that you can't
for 1, I wonder what's the use case for needing a synchronous update? Deriveds are only rerun when they're read and it would really only happen inside an effect if state changes. for 2, I would say that it's acceptable to use |
You can't bind to the derived itself but you should bind to it's property anyway.
You can read a derived also outside an effect....if you read a linked prop the line after setting the prop with derived you would get the right value, with effect the wrong one.
It's way better to do the derived thing |
right, just assumed the whole thing would be passed in, e.g. if there were more props in the derived vs one by one. I was trying an example where the state was created outside of the
If you need to read it in the instance code then yeah for sure and during ssr. Otherwise, I don't see a problem as long as one understands how Implementing it this ways does seem somewhat cumbersome, I wonder if we had a rune e.g. let count = $state(0);
let state = $state.derived(() => {
count; // read dependencies, function runs on the first read and on reads if dependencies change
return count; // or specify whatever conditions make sense, but able to reference itself (state in this case) vs $derived
}); |
It's not only for SSR and during instance (which are also important) Changes to derives are immediately live, the same is not true for effects |
that's an interesting use case with events, they do fire before the @paoloricciuti what do you think of the idea to create
|
That's basically |
right, I just didn't want to name it that since the "api" (and implementation) is different than the one that was proposed originally. Definitely agree that it's basically syntactic sugar but it does make life easier especially for less experienced svelte devs. |
Describe the bug
I've been using this pattern from a @PatrickG's comment and I consider it a decent userland-implementation for
$state.link
:It worked fine in earlier versions of Svelte 5. However, after the performance patch in PR #14533, object ownership isn't added for
$state
inside$derived
anymore. Quote:As a consequence, trying to use this pattern across component boundaries, either via the context API or props will produce the
ownership_invalid_binding
warning, even with proper prop binding. I've seen people asking problems that can be solved perfectly by this pattern, but I'm hesitant to recommend it to them. Sometimes I offer an alternative solution using$effect
instead and they're happy to use it because there's no warning. However I feel bad about using$effect
because 1. it's not synchronous and 2. updating states inside it is considered a bad practice.Recently I saw a @paoloricciuti 's comment regarding this pattern and it may be even added to the docs. I'm relieved that it's a valid pattern that is recognized by the team. I feel like it's time the revisit the ownership check for this case again, especially after the recent PR #15153.
Reproduction
https://svelte.dev/playground/d51b66bbb70e413498fe70b23944fcd7?version=5.19.6
Logs
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: