-
-
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
Array mutation doesn't update #2362
Comments
See the issue #2243 |
Yep, this is by design. Svelte doesn't treat |
Thanks, guys. I momentarily thought arrays might deserve special treatment, considering the array mutation is such a common pattern, but realized it is straightforward concept once we learn why. I also love the workaround - simple and intuitive. |
This may seem odd for array mutation, but it's a reasonable design. |
You can use: arr = [...arr, newValue]; This will trigger an update. Unfortunately you're also creating an entirely new array. Not the neatest solution but it works. |
Hi! I don't really know where to post my related question, but here it is... I'm used to working immutably in Javascript, especially using the Ramda library. In Svelte, is it idiomatic, when updating an array or object, to make a new version immutably and only reassign it to the same variable?
@AltarnRain Why do you say it is unfortunate? Performance problems? And what would be the neatest solution then? What are the performance implications if, in the example below, I change the value of entity let entitiesById = {
foo: { _id: "foo", value: 1 },
bar: { _id: "bar", value: 2 },
};
const modifiedBar = R.pipe(
R.prop("bar"),
R.assoc("value", 3)
)(entitiesById);
entitiesById = R.assoc("bar", modifiedBar, entitiesById);
// {
// foo: { _id: "foo", value: 1 },
// bar: { _id: "bar", value: 3 },
// } Thanks a lot! |
The documentation says: "Because Svelte's reactivity is based on assignments, using array methods like .push() and .splice() won't automatically trigger updates. ". But doesn't an assignment occur internally, behind the scenes of the .push() method? And therefore, following the logic, an update should be triggered? Or do we ignore the internals of build-in functionality? Or maybe something else is going on? This confuses me a bit. |
@JustinRademaker1994 In svelte 5 array mutation methods work :) |
First of all, I finished v3 tutorial and Svelte is amazing. Thanks for making this.
I am exploring Svelte's internals and found a behavior that I couldn't understand.
https://v3.svelte.technology/repl?version=3.0.0-beta.22&gist=1d7388842c1b334366fd60f53fc5c2d9
In default
immutable: false
mode, The array mutation doesn't update whereas object mutation does. Is this by design? I am using3.0.0-beta.22
.The text was updated successfully, but these errors were encountered: