Skip to content
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

Unexpected reactivity #3065

Closed
MrRobu opened this issue Jun 21, 2019 · 7 comments
Closed

Unexpected reactivity #3065

MrRobu opened this issue Jun 21, 2019 · 7 comments

Comments

@MrRobu
Copy link

MrRobu commented Jun 21, 2019

View in REPL

@Axelen123
Copy link
Contributor

It's because svelte reacts when you reassign (example: array = array[..array, value]), not when you update the value (example: array.push(value)).

Example REPL

@btk5h
Copy link
Contributor

btk5h commented Jun 21, 2019

Self-assignment is a documented workaround to force Svelte to update mutable objects.

You can use <svelte:options immutable/> to make Svelte use referential equality checks for objects. REPL

@MrRobu
Copy link
Author

MrRobu commented Jun 21, 2019

It's because svelte reacts when you reassign (example: array = array[..array, value]), not when you update the value (example: array.push(value)).

Example REPL

But why reacts if is the same value (pointer to that array)? And why reacts to the same value only to arrays and not other value types?

@MrRobu
Copy link
Author

MrRobu commented Jun 21, 2019

Self-assignment is a documented workaround to force Svelte to update mutable objects.

You can use <svelte:options immutable/> to make Svelte use referential equality checks for objects. REPL

Ok, so that tag is the solution for the select. But why it reacts if I don't touch the array when i select an item?

@Axelen123
Copy link
Contributor

But why reacts if is the same value (pointer to that array)?

Svelte doesn't store and compare the value. When you write this:

let value = "something";
value = "foo";

The svelte compiler turns it into something like this:

let value = "something";
value = "foo"; $$invalidate("value");

$$invalidate() basically tells svelte that the variabler was reassigned.

And why reacts to the same value only to arrays and not other value types?

The type does not matter.
I could make an example with objects but I an on my phone right now :/

@MrRobu
Copy link
Author

MrRobu commented Jun 21, 2019 via email

@btk5h
Copy link
Contributor

btk5h commented Jun 21, 2019

Checkout the source code for safe_not_equal. Objects are always considered unequal unless you use the immutable option to allow referential equality for objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants