-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Introduce $state.from
rune
#12956
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
Could this also be used for mapping a store to state? |
You can already do that using |
No idea how i missed that, thanks! |
It strongly reminds me of #9237, with a bit of another shape. Or
see #11014 Also, I find it very strange that with the proposed solution, you can create a variable with a primitive value but with attached logic. let array = $state([1, 2, 3]);
let length = $state.from({
get: () => array.length,
set: (value) { array.length = value },
);
length = 100; It doesn't really make sense to me. If a value has a logic, use an object or class. |
This proposal is specifically to allow external things to be "sveltified" without going down the rabbit hole of allowing users to modify the compiler.
Isn't that exactly what a variable declared with |
Not really. It is about a more convenient boilerplateless way of using external stateful things. But it's strongly related to the API design and use cases, so it's a tricky topic.
Changing a variable defined with $state does nothing by itself, but there can be effects and derives that will react to this change. I've used writable computed, so I won't say it's unneed. But using it for libs and utils isn't good because the result isn't extendable - you cannot add a new prop to a primitive value. So, it makes sense to return an object anyway. And as for your abstract example with localStorage, here is my implementation, which I then use as simply |
But all the logic that tracks/triggers those things does run, it's just that logic is written by the authors of Svelte so we trust it more.
The writeable derived is just one use case, which is why I didn't call it that. I think it's better to keep
That depends on what your library is providing. If you have something more complex, then yes, a class makes more sense. If not, then
The thing I like about this example with |
This makes sense to me, it looks like it boils down the minimal I put a lot of weight on the compatibility with the proposed standard, if the API doesn't take a hit - I like this fit. |
Describe the problem
People want to be able to define higher-order states. This could be an escape hatch to allow them to do it.
Describe the proposed solution
Introduce a
$state.from
rune that takes an object withget
andset
methods. Compiler then simply appends the.get()
and.set(value)
methods where appropriate when the variables is read from/written to (or does something trickier to optimise).This would enable:
$derived.writable
use caseExamples
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: