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
Svelte 5: Local variables in the markup using @let #9896
Comments
This would be an anti pattern, as Svelte prioritizes separating state and logic from templates. So, putting additional state in the template would make tracking bugs more difficult. To accomplish what you're trying to do the Svelte way, you just want an array of counts, and each item updates its entry in the array. @const is just a convenience to help you not rewrite the same property over and over, not a way to declare additional state or logic. <script>
const { totalCount } = $props()
let counts = $state(new Array(totalCount).fill(0))
</script>
{#each items as item, index}
{counts[index]}
<button onclick={() => counts[index]++}> Increment </button>
{/each} |
Solid response. I guess I'm coming from the point of view of highly interactive systems and rapid iteration, where touching two or more places to add functionality has become a hurtle as I develop my apps. I won't put my self out there but I'm making a timeline/video/editor (many instances per client) that can be hosted on clients/websites in real time. I knew I reached a threshold with svelte when the bundle sizes were unmanageable and code splitting was stopping development. With the arrival of Runes my problems could go away but I feel like the Svelte language itself changed its ways, I'm referring to the abandonment of the dollar sign reactivity and "let" variables. This feels reminiscent of the transition from class components to hooks in React and devs where forced to rewrite their apps to keep up to date. Right know I'm on a journey of swapping Svelte with a new framework - Am I crazy? Probably. But I'm treading carefully while using https://mitosis.builder.io/?outputTab to compile my existing svelte components to more performant framework with great primitives SolidJS or the framework that is years ahead of everyone else MarkoJS, if you saw that meme "Marko did it first" is because it did, in fact runes is almost exactly to what Marko was doing years ago. |
It's a good suggestion, just probably antithetical to how Svelte wants to do things. You may want to look at state classes in Svelte 5 for those "bundles" of state, or reorganize your components so the state that would otherwise be in |
Describe the problem
Right know the
@const
tag is too restrictive. It got better over time but still #7241Describe the proposed solution
This is marko's tag api syntax example but the svelte equivalent would be
{@let = ... }
. The idea is to have local variables in the markup.With the arrival of Svelte 5 Runes (fined grained reactivity) it makes sense to describe intent where it's needed, the current approach is to hoist variables at the script level and use them down in the markup.
Alternatives considered
The alternative is to write crazy code :v
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: