-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Runes: get
/set
syntax / creating reactive #each
items sucks
#9240
Comments
Just realized that #9237 covers similar ground regarding Still, I also do question whether |
It's a crucial point indeed. |
One of the reasons for runes was to have more fine-grained reactivity, where it would not be necessary to invalidate/update the entire list when one item changes (see intro video / preview docs). This transform would bring back the previous level of reactivity but also invalidate the entire list.
That being the case, this probably has to be approached differently:
|
In Discord Xwth noted that having class Product {
id = $state(crypto.randomUUID());
name = $state('New Product');
count = $state(0);
increment() { this.count++; }
decrement() { this.count--; }
} This simply compiling to accessors also seems useful and reasonable. For the to-do example that would yield something like: class Todo {
done = $state(false);
text = $state();
constructor(text) { this.text = text }
}
let todos = $state([
new Todo('item 1'),
new Todo('item 2'),
]); If this were to be extended to plain objects, the code really would be rather svelte again: let todos = $state([
{ text: $state('Item 1'), done: $state(false) },
{ text: $state('Item 2'), done: $state(false) },
]); (For storing the signals, the class could use private fields or a symbol, the plain object could be compiled to an IIFE that closes over a local variable for the signal and returns an object with accessors.) |
@brunnerh okey, it's ugly but works, what we should do if the source TODO list comes from the server and we serialize it just from JSON? |
@stalkerg You would either have to map it manually to add state or use a utility function to do so. There already has been some discussion on such a function and some version of that will probably be included with Svelte itself. |
@brunnerh unfortunately, we have no shape of this function yet. It's definitely solvable, but I feel this issue needs more attention. |
Addressed by: |
Describe the problem
I just wanted to port a simple to-do sort of demo to runes but it was more difficult than expected and the end result is more verbose than a "Svelte" component should be. Of course I might be doing something wrong.
First naive approach was just one state:
The bindings in the
#each
block do nothing.Next step was trying to use a state for each item:
Which does not work because the
item
gets evaluated in the compiled output before it is returned.So finally, every changing property is stateified:
This finally works but is ...not great, even if we could use arrow functions (as already bemoaned by Rich Harris).
Is this just buggy or will it really be necessary to use
$state
for every single property?I imagine this being an issue with JSON returned from a server.
Describe the proposed solution
If it is necessary to declare that many properties, that could probably be done by the compiler, e.g. with an additional rune.
Should then be spreadable, too, e.g.
$access
could possibly be split into something like$readable
/$writable
to control whether setters are generated.Alternatives considered
$state
and bind to that indexed:Importance
nice to have
The text was updated successfully, but these errors were encountered: