-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Reactive statements have incorrect values in functions where dependent values are updated #4586
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
Comments
I too have been confused by behavior like this. Perhaps a clearly defined way to isolate atomic units with synchronous reactivity would help those of us still working through the idiosyncrasies of reactivity. |
We have discussed this on Discord I believe. Personally I think this is the expected behavior. It follows from how Javascript and the browser works, I think. Having it the other way could lead to strange bugs. What could be good is a clarifying statement in the documentation. |
@kevmodrome Yes, we have discussed it but I still don't think that this should be the behavior. |
For performance reasons, If you want something that updates synchronously and depends on another value, you can use a derived store: <script>
import { writable, derived } from 'svelte/store';
const foo = writable(0);
const even = derived(foo, $foo => $foo % 2 === 0);
console.log($foo, $even);
$foo = 1;
console.log($foo, $even);
</script> |
@Conduitry Oh, wow, it works like a charm! Btw, I didn't know that you can directly assign values to stores like this.
I thought that you should always use |
Easy to miss, but yes it's official syntax and documented at https://svelte.dev/docs#4_Prefix_stores_with_$_to_access_their_values |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This will be solved in Svelte 5 through the new |
Describe the bug
Reactive statements have incorrect values in functions where dependent values are updated.
Example:
To Reproduce
REPL
Expected behavior
Reactive statements should have correct (updated) values even if used in a function where their dependent values are updated.
Severity
Critical - I think the current behavior is confusing and might lead to bugs. In fact, I have found this as I was having some weird bugs in production. In addition, using
tick()
to get the expected behavior in this case is odd and leads to code bloating.The text was updated successfully, but these errors were encountered: