#each feels unnecessarily complex and far from JS #14133
mindplay-dk
started this conversation in
Ideas
Replies: 1 comment 9 replies
-
Changes to As for how a keyed |
Beta Was this translation helpful? Give feedback.
9 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I decided to learn Svelte 5 and I'm just going over the tutorial.
The
{#each}
construct is extremely perplexing to me and feels unnecessarily distant from anything resembling native JS.Problem
Let me explain with an example:
First of all, looping over object properties is common enough in JS, and this was hard to figure out, first of all, since
#each
works like afor..of
and can't be used like afor..in
, so you need to manually callObject.entries
to turn the object into an array - which, besides being unintuitive, makes me wonder about the performance.Then there's the
(key)
syntax. I legit cannot figure out what these expressions are relative to? It seems the key expression can reference a symbol you've already defined on the right-hand side ofas
- but it also accepts expressions likename[0]
, which I assumed would mean e.g. first letter ofname
? But the error message suggests something completely different:It appears the key is being set to the whole tuple value and
name[0]
does not actually do anything?name[0][0]
produces the exact same error, so it would appear this syntax is just being ignored entirely.Even for the plain array loop, I am not confident that
(index)
does what I think it does.I've gone over the docs and the tutorial again and again, and I can't make any sense of it.
Suggestion
Why don't we align this better with JS terminology and behavior?
So for objects, make this directive more closely resemble the
for..in
syntax:Of course, a regular
for..in
only acceptname
and notname, value
, but I think that's a fairly intuitive enhancement - something that's frankly just missing in JS proper.As for the
(name)
suffix, obviously this isn't plain JS either - and, honestly, I would give real consideration to just omitting this feature. Just make me use a{#key}
block instead - that would be fine. If you do have this feature, I would expect(name)
has to reference something you've already defined or destructured in the name/value expressions. (I still don't understand how the key expression is evaluated in the current{#each}
directive.)If this internally needs to call
Object.entries
, or add the boilerplateObject.hasOwnProperty
sanitization, that's an implementation detail that's very unlikely to get in anybody's way.Similarly, for arrays, make this directive more closely resemble the
for..of
syntax:Again, the only deviation is the addition of the
value, index
pair, something JS should have had - and the(index)
key reference, which I'd be perfectly happy to get rid of.Small thing, but the
{:else}
directive is going to make a lot more sense in conjunction with{#if}
as well:The presence of little keywords like
in
,of
andas
in the same file is just confusing - these words have very weak semantics and it's just too easy to get these mixed up and get confused. We can't get rid ofin
andof
, and JS developers already understand these, but I feel things get muddy when we throwas
into the mix as well.Working with a template language, you're going to be going back and forth between JS code and templates a lot - for something as fundamental and essential as loops, wouldn't it make sense to align the terminology and behavior more closely with the language you're using the other half of the time, when you're not writing templates?
Contrast this with
{#if}
, which is immediately intuitive - it looks and works just like a regularif..else
statement, only with the minor addition of the template block syntax. (something I've wished JSX would have had, since forever.)By comparison, the
{#each}
directive deviates from JS in so many ways besides just the block syntax.It just feels like unnecessary learning curve for something everyone already knows from JS.
Thoughts? 🤔
Beta Was this translation helpful? Give feedback.
All reactions