-
-
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
Weird display with array reverse method #7295
Comments
This has come up multiple times. You are mutating state inside the template expression, which is kind of undefined behavior. Something that in my opinion should straight up not compile in Svelte 4. You are relying on the order in which Svelte decides to do things, which is not necessarily from top to bottom because this is not a templating language but a living application. Svelte can do things in whatever order it thinks is most efficient. Someone with knowledge about internals can probably tell you exactly why you are seeing what you are seeing, which could actually be different between Svelte versions. In fact check out 3.0.0 https://svelte.dev/repl/2b54f8e14ae7455d8f653cc954d32c50?version=3.0.0 Related |
Yup, changing component state inline in the template is undefined behavior. Preventing this at compile time would be nice to do in Svelte 4 where possible, but we're not going to be able to catch cases like this where there's no visible assignment and instead the state change happens in a method that mutates state. |
@Conduitry @Prinzhorn if we want to avoid arbitrarily supporting inline logic like this, what if Svelte added support for helper statements/blocks similar to <script>
let arr = [1, 2, 3];
let index = 0;
</script>
{#each arr as elem, i}
{@set index } <!-- or {@set index = 1} or {@set index = index 1} -->
This {index === i ? 'works' : 'doesn\'t work'} when `i` equals {i}, where `index` equals {index}.
{/each} |
Giving it its own syntax doesn't resolve any of the concerns with how it should behave. |
That's still undefined behavior. When would If you think you have a use-case that Svelte does not support in a nice way and that doesn't already have an open or clsoed feature request, then please provide such a feature request. But in a more detailed way than your comment (the feature request issue template will guide you). |
@Prinzhorn Of course. Just contributing to the discussion here. I see your point about the template being alive and the declarative vs. imperative conflicts something like |
Describe the bug
I know that reverse() is array mutable method, but I'm not sure. Is this a bug or not?
Why is the display different between these 2 examples? (note the display of original array)
array reverse() in text fragment
Compare with
weird array reverse in html block element
Reproduction
as shown in the captured screen above
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: