-
-
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
New runes $bind() / $read() for easier access to props/state #9951
Comments
Isn't wrapping the state into an object easier? Also thus, the state is extendable to additional data without breaking code. |
Also thought about Related: |
It's more verbose and less pratical, and will impact the template : <script>
let ref = $state({count:0});
fn(ref);
</script>
{ref.count} VS <script>
let count= $state(0);
fn($bind(count));
</script>
{count} |
You don't necessarily have to wrap it from the start but can wrap it while passing it to the function. |
I find it pretty impractical to having to use yet another rune to wrap a rune just to pass it along tbh. |
What about bypassing a pointer to a state? function createCounter() {
let count = $state(0);
return $pack(count);
}
let count = $state.from(createCounter()); will compile to const privateGlobalMap = new WeakMap();
function createCounter() {
let rawSignal = $state();
let key = Symbol(); // or just {}
privateGlobalMap.set(key, rawSygnal);
return key;
}
let count = privateGlobalMap.get(createCounter()); Thus, the signal objects aren't exposed but can still be directly passed. However, there is the question of what should happen when you $pack a non-signal or do $state.from for not a packed signal. |
Closing for the reasons given in #9237 (comment) |
Describe the problem
$state()
/$props()
are very practical within *.svelte component files, but it may be difficult to use in others files (.js/.ts
or even.svelte.js/.ts
), because we have no way to pass the state himself, but only the value.For example take this code :
I will produce a warning :
The solution is to use closures, so
But it's a little more verbose and less readable.
Describe the proposed solution
A solution could be to define runes to automate this.
For example
$read()
for a read-only access, and$bind()
for a read/write access.$read()
will simply return a closure$bind()
will return the same closure, containing an additional attribute for update.The return type of these runes will match this (expressed in TypeScript) :
So :
And inside the function we have a closure that we can use to access the value of the element :
Example with an action that update state : REPL
Alternatives considered
using separate closure like now
Importance
nice to have
The text was updated successfully, but these errors were encountered: